!747 m { static int threads = 10; static int timeout = 500; // SHOULD be enough... static String ip = "127.0.0.1"; p { final ExecutorService es = Executors.newFixedThreadPool(threads); print("Port scanning " + ip + " with timeout " + timeout + " ms in " + threads + " threads."); startTiming(); new List> futures; for (int port = 1; port <= 65535; port++) { futures.add(portIsOpen(es, ip, port, timeout)); } es.shutdown(); new List openPorts; for (final Future f : futures) { int port = f.get(); if (port != 0) openPorts.add(port); } stopTiming(); System.out.println("There are " + openPorts.size() + " open ports on host " + ip + ": " + structure(openPorts)); } public static Future portIsOpen(final ExecutorService es, final String ip, final int port, final int timeout) { return es.submit(new Callable() { @Override public Integer call() { try { Socket socket = new Socket(); socket.connect(new InetSocketAddress(ip, port), timeout); print("Connected to " + ip + ":" + port); socket.close(); return port; } catch (Exception ex) { return 0; } } }); } }