지금보다 빠르게 진행하려고합니다. 그것은 느린 슈퍼입니다, 스레드가 동시에 이동하지 않는 것, 그것을 알아낼 수 없습니다. 누군가가 내 문제가 어디 있는지 설명하여 도움을 줄 수 있다면 더 빨리 진행할 수있는 방법을 찾아 낼 수있어서 정말로 감사 할 것입니다. 정말 고마워요!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();
}
}
스레드를 사용하지 마십시오. 'ExecutorService'를 사용하십시오. 'ExecutorService'가있을 때 자신의 쓰래드를 만들고 관리하는 것을 정당화하는 것은 거의 없습니다. 그러나 스캐너가 느린 이유 중 가장 큰 이유는 소켓에 낮은 시간 제한을 설정하지 않았기 때문입니다. – biziclop
'thread.start(); thread.join();'은 병렬 처리를 완전히 제거합니다. 이전 스레드가 완료 될 때까지 다음 스레드가 시작되지 않습니다. – Arkadiy