以上程序需要4个参数,输入java PortScanner 10.1.1.182 1 10000 100运行(第4个参数是线程数),结果前两个程序一样,但是速度比第一个要快,可能比第二个要慢一些。 
						
							
   以上程序需要4个参数,输入java PortScanner 10.1.1.182 1 10000 100运行(第4个参数是线程数),结果前两个程序一样,但是速度比第一个要快,可能比第二个要慢一些。
    第3个程序是把端口作为“池”中的对象,下面我们看第4个实现方式,把“池”里面的对象定义成是线程类,把具体的任务定义成”池“中线程类的参数。第4个程序有2个文件组成,分别是ThreadPool.java和PortScannerByThreadPool.java.
    ThreadPool.java文件内容如下:
-----------------------------------------------------------
import java.util.LinkedList;
public class ThreadPool{
    private final int nThreads;
    private final PoolWorker[] threads;
    private final LinkedList queue;
    public ThreadPool(int nThreads){
        this.nThreads = nThreads;
        queue = new LinkedList();
        threads = new PoolWorker[nThreads];
        for (int i=0; i<nThreads; i++) {
            threads[i] = new PoolWorker();
            threads[i].start();
        }
    }
    public void execute(Runnable r) {
        synchronized(queue) {
            queue.addLast(r);
            queue.notifyAll();
        }
    }
    private class PoolWorker extends Thread {
        public void run() {
            Runnable r;
            while (true) {
                synchronized(queue) {
                    while (queue.isEmpty()) {
                        try{
                            queue.wait();
                        }catch (InterruptedException ignored){
                        }
                    }
                    r = (Runnable) queue.removeFirst();
                }
                try {
                    r.run();
                }
                catch (RuntimeException e) {
                }
            }
        }
    }
}
------------------------------------------------------------------------------------------------------------------
    在ThreadPool.java文件中定义了2个类:ThreadPool和PoolWorker。ThreadPool类中的nThreads变量表示线程数,PoolWorker数组类型的threads变量表示线程池中的“工人”,这些“工人”的工作就是一直循环处理通过queue.addLast(r)加入到“池”中的任务。
    PortScannerByThreadPool.java文件内容如下:
-------------------------------------------------------------------------------------------------------------------
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
public class PortScannerByThreadPool {
    public static void main(String[] args) {
        String host = null;
        int beginport = 1;
        int endport = 65535;
        int nThreads = 100;
        try{
            host = args[0];
            beginport = Integer.parseInt(args[1]);
            endport = Integer.parseInt(args[2]);
            nThreads = Integer.parseInt(args[3]);
            if(beginport <= 0 || endport >= 65536 || beginport > endport){
                throw new Exception("Port is illegal");
            }
        }catch(Exception e){
            System.out.println("Usage: java PortScannerSingleThread host beginport endport nThreads");
            System.exit(0);
        }        
        ThreadPool tp = new ThreadPool(nThreads);        
        for(int i = beginport; i <= endport; i++){
            Scanner ps = new Scanner(host,i);
            tp.execute(ps);
        }
    }
}
查看本文来源