2014-09-23 3 views
0

지금보다 빠르게 진행하려고합니다. 그것은 느린 슈퍼입니다, 스레드가 동시에 이동하지 않는 것, 그것을 알아낼 수 없습니다. 누군가가 내 문제가 어디 있는지 설명하여 도움을 줄 수 있다면 더 빨리 진행할 수있는 방법을 찾아 낼 수있어서 정말로 감사 할 것입니다. 정말 고마워요!Java 다중 스레드 포트 스캐너

package infoGrabber; 

import java.awt.List; 
import java.io.IOException; 
import java.net.Socket; 
import java.util.Scanner; 

public class infoMain { 

    public static int port; 

    @SuppressWarnings("resource") 
    public static void main(String[] args) { 
     System.out.println("What host do you want to lookup?: "); 
     Scanner userEntry = new Scanner(System.in); 
     String host = userEntry.nextLine(); 

     try { 
      startThreads(host); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 

    } 

    private static void startThreads(String host) throws InterruptedException { 
     int numThreads = 10; 
     int count = 10; 
     Thread[] threads = new Thread[numThreads]; 
     System.out.println("Creating threads"); 
     for (int i = 0; i < threads.length; i++) { 
      threads[i] = new Thread(new Runner(host, count)); 
      threads[i].start(); 
      threads[i].join(); 
     } 
     System.out.println("Done"); 
    } 
} 

class Runner implements Runnable { 
    static int port; 
    private final String host; 
    private final int count; 
    infoMain main = new infoMain(); 

    public Runner(String host, int count) { 
     this.host = host; 
     this.count = count; 
    } 

    public void run() { 
     for (int port = 0; port < 2000; port++) { 
      // System.out.println(name + "=" + i + "\n"); 
      Socket socket; 
       try { 
        socket = new Socket(host, port);// Attempt to establish a socket on port i. 
        // If no IOException thrown, there must 
        // be a service running on the port. 
        System.out.println("Port " + port + " is open."); 
        socket.close(); 
       } catch (IOException ioEx) { 
        System.out.println("Port " + port + " is not open."); 
       }// No server on this port 
      } 
      Thread.yield(); 
     } 
    } 
+3

스레드를 사용하지 마십시오. 'ExecutorService'를 사용하십시오. 'ExecutorService'가있을 때 자신의 쓰래드를 만들고 관리하는 것을 정당화하는 것은 거의 없습니다. 그러나 스캐너가 느린 이유 중 가장 큰 이유는 소켓에 낮은 시간 제한을 설정하지 않았기 때문입니다. – biziclop

+4

'thread.start(); thread.join();'은 병렬 처리를 완전히 제거합니다. 이전 스레드가 완료 될 때까지 다음 스레드가 시작되지 않습니다. – Arkadiy

답변

1

스레드간에 작업을 나누지 않습니다. 대신 순차 프로그램의 주 스레드에 부여하는 것과 같이 각 스레드에 동일한 양의 작업을 제공합니다. 실행 속도를 높이려면 스레드간에 작업을 나누어야합니다.

루프는과 같이 보일 것입니다 :

for (int i = 0; i < threads.length; i++) { 
    threads[i] = new Thread(new Runner(host, count * NUMBER_OF_PORTS_PER_THREAD)); 
    threads[i].start(); 

} 

// Join threads after you created them 
// All credits for @biziclop for spotting this 
for (int i = 0; i < threads.length; i++) { 
    threads[i].join(); 
} 

을 그리고 당신의 Runnable 코드 보일 것 같은 뭔가 :

NUMBER_OF_PORTS_PER_THREAD 10 개 스레드 2000 포트를 스캔 200 같아야한다
class Runner implements Runnable { 

final private int startPort; 

public Runner(String host, int startPort) { 
     this.host = host; 
     this.startPort = startPort; 
    } 

    public void run() { 
     for (int port = startPort; port <= NUMBER_OF_PORTS_PER_THREAD + startPort; port++) { 
     ... 
    } 
} 

.

+2

하지만'join()'을 삭제해야합니다. – biziclop

+1

@biziclop 고마워요! 내 대답을 업데이트했습니다. –