Попытка заставить его двигаться быстрее, чем сейчас. это супер медленно, нити, похоже, не идут в одно и то же время, не могу понять это. Если бы кто-нибудь мог помочь описать, где моя проблема, чтобы я мог понять, как это сделать быстрее, я бы очень признателен, спасибо вам большое!Сканер многопоточного порта 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