JAVA服务器端Socket线程池

  1. import java.util.Vector;
  2. import java.net.*;
  3. import java.io.*;


  4. public class ThreadPool {
  5.     public static final int MAX_THREADS = 100;
  6.     public static final int MAX_SPARE_THREADS = 50;
  7.     public static final int MIN_SPARE_THREADS = 10;
  8.     public static final int WORK_WAIT_TIMEOUT = 60 * 1000;

  9.     protected Vector pool; //存放空闲线程
  10.     protected MonitorRunnable monitor; //A monitor thread that monitors the pool for idel threads.
  11.     protected int maxThreads; //Max number of threads that you can open in the pool.
  12.     protected int minSpareThreads; //Min number of idel threads that you can leave in the pool.
  13.     protected int maxSpareThreads; //Max number of idel threads that you can leave in the pool.
  14.     protected int currentThreadCount; //Number of threads in the pool.
  15.     protected int currentThreadsBusy; //Number of busy threads in the pool.
  16.     protected boolean stopThePool; //Flag that the pool should terminate all the threads and stop.

  17.     /**
  18.      * Construct
  19.      */
  20.     public ThreadPool() {
  21.       maxThreads = MAX_THREADS;
  22.       maxSpareThreads = MAX_SPARE_THREADS;
  23.       minSpareThreads = MIN_SPARE_THREADS;
  24.       currentThreadCount = 0;
  25.       currentThreadsBusy = 0;
  26.       stopThePool = false;
  27.     }

  28.     /**
  29.      * 启动线程池
  30.      */
  31.     public synchronized void start() {
  32.       adjustLimits(); //调整最大和最小线程数及最大和最小多余线程数.
  33.       openThreads(minSpareThreads); //打开初始线程
  34.       monitor = new MonitorRunnable(this); //Runnable对象实例     //A monitor thread that monitors the pool for idel threads.
  35.     }

  36.     public void setMaxThreads(int maxThreads) {
  37.       this.maxThreads = maxThreads;
  38.     }

  39.     public int getMaxThreads() {
  40.       return maxThreads;
  41.     }

  42.     public void setMinSpareThreads(int minSpareThreads) {
  43.       this.minSpareThreads = minSpareThreads;
  44.     }

  45.     public int getMinSpareThreads() {
  46.       return minSpareThreads;
  47.     }

  48.     public void setMaxSpareThreads(int maxSpareThreads) {
  49.       this.maxSpareThreads = maxSpareThreads;
  50.     }

  51.     public int getMaxSpareThreads() {
  52.       return maxSpareThreads;
  53.     }

  54.     /**
  55.      * 线程池管理方法.
  56.      * 当空闲队列线程中没有空闲线程时,则增加处理(空闲)线程数量.
  57.      * 如果线程数量已达到最大线程数,则新的连接进行等待.
  58.      * 当请求到来,且有空闲线程时调用处理线程进行具体业务处理.
  59.      * @param r ThreadPoolRunnable
  60.      */
  61.     public void runIt(Socket cs) { //r 为task      //有任务进入时调用
  62.       if (null == cs) {
  63.         throw new NullPointerException();
  64.       }
  65.       if (0 == currentThreadCount || stopThePool) {
  66.         throw new IllegalStateException();
  67.       }
  68.       ControlRunnable c = null//任务处理实例.
  69.       synchronized (this) {
  70.         if (currentThreadsBusy == currentThreadCount) { //如果工作线程和当前线程数相等,说明没有空闲线程.
  71.           if (currentThreadCount < maxThreads) { //如果当前线程数还没有达到最大线程数.
  72.             int toOpen = currentThreadCount + minSpareThreads; //再增加minSpareThreads个线程量.
  73.             openThreads(toOpen); //打开线程新增空闲线程.    //currentThreadCount数量增加
  74.           }
  75.           else { //如果当前数量达到了最大线程数.
  76.             while (currentThreadsBusy == currentThreadCount) { //当工作线程和当前线程数相等,说明没有空闲线程.
  77.               try {
  78.                 this.wait(); //连接线程进行等待.
  79.               }
  80.               catch (InterruptedException e) {
  81.               }
  82.               if (0 == currentThreadCount || stopThePool) {
  83.                 throw new IllegalStateException();
  84.               }
  85.             }
  86.           }
  87.         }
  88.         c = (ControlRunnable) pool.lastElement(); //在有空闲线程的情况下,从空闲线程队列中取出最后一个线程.
  89.         pool.removeElement(c); //从空闲队列中删除最后一个线程,用于处理其他事件.
  90.         currentThreadsBusy++; //对处理事件的线程数加1
  91.       }
  92.       System.out.println("系统调用一个Sokcet线程");
  93.       c.runIt(cs); //调用具体业务方法,告诉其有数据请求要处理,唤醒等待中的线程.
  94.     }

  95.     /**
  96.      * 关闭线程池
  97.      */
  98.     public synchronized void shutdown() {
  99.       if (!stopThePool) { //如果线程池没有关闭,(线程池关闭标识为假)
  100.         stopThePool = true;
  101.         monitor.terminate(); //关闭监视线程
  102.         monitor = null;
  103.         for (int i = 0; i < (currentThreadCount - currentThreadsBusy); i++) { //关闭空闲线程队列
  104.           try {
  105.             ( (ControlRunnable) (pool.elementAt(i))).terminate();
  106.           }
  107.           catch (Throwable t) {
  108.           }
  109.         }
  110.         currentThreadsBusy = currentThreadCount = 0;
  111.         pool = null;
  112.         notifyAll(); //唤醒所有在等待的线程.
  113.       }
  114.     }

  115.     /**
  116.      * 当线程大于最大多余线程时关闭多余的线程.
  117.      */
  118.     protected synchronized void checkSpareControllers() {
  119.       if (stopThePool) { //如果连接池没有关闭.
  120.         return;
  121.       }

  122.       if ( (currentThreadCount - currentThreadsBusy) > maxSpareThreads) { //如果空闲的线程数大于多余的最大线程数量.
  123.         int toFree = currentThreadCount - currentThreadsBusy - maxSpareThreads; //得出多余的线程数量
  124.         for (int i = 0; i < toFree; i++) { //关闭删除空闲线程,从Vector中删除
  125.           ControlRunnable c = (ControlRunnable) pool.firstElement();
  126.           pool.removeElement(c);
  127.           c.terminate(); //让删除的线程结束
  128.           currentThreadCount--; //处理线程队列减少一个
  129.         }
  130.       }
  131.     }

  132.     /**
  133.      * 当线程处理完成后重新放到空闲线程队列中.
  134.      * @param c ControlRunnable
  135.      */
  136.     protected synchronized void returnController(ControlRunnable c) {
  137.       if (0 == currentThreadCount || stopThePool) { //如果线程池关闭或当前连接线程数量为0
  138.         c.terminate(); //关闭当前线程.
  139.         return;
  140.       }
  141.       currentThreadsBusy--; //处理线程队列的数量减少一个
  142.       pool.addElement(c); //空闲线程队列中增加一个
  143.       notifyAll(); //唤醒可能在等待连接的线程.
  144.     }

  145.     /**
  146.      * 当一个处理线程出现异常时,要重新开启一个空闭线程.,并唤醒在等待空闲线程的线程.ThreadPool的runIt中等待的线程.
  147.      */
  148.     protected synchronized void notifyThreadEnd() {
  149.       currentThreadsBusy--; //因从线程是在处理数据时出现异常,所处理线程队列的数量要减一个.
  150.       currentThreadCount--; //因出现异常的线程关闭了.所开户线程的数量要减少一个.
  151.       notifyAll(); //唤醒等待连接的阻塞线程.
  152.       openThreads(minSpareThreads); //重新打开minSpareThreads个线程.如currentThreadCount的数量大于minSpareThreads,则还是不开启新线程.
  153.     }

  154.     /**
  155.      * 调整各种线程队列数量
  156.      */
  157.     protected void adjustLimits() {
  158.       if (maxThreads <= 0) { //如果最大线程数小于0
  159.         maxThreads = MAX_THREADS; //设置最大线程数为100
  160.       }
  161.       if (maxSpareThreads >= maxThreads) { //如果最大多余线程数大于最大线程数.
  162.         maxSpareThreads = maxThreads; //设置最大多余线程数为最大线程数.
  163.       }
  164.       if (maxSpareThreads <= 0) { //如果最大多余线程数小于0
  165.         if (1 == maxThreads) {
  166.           maxSpareThreads = 1; //如最大线程数为1的情况下,设置最大多余线程数为1.
  167.         }
  168.         else {
  169.           maxSpareThreads = maxThreads / 2; //设置最大多余线程数为最大线程数的一半.
  170.         }
  171.       }
  172.       if (minSpareThreads > maxSpareThreads) { //如果最小多余线程大于最大多余线程数
  173.         minSpareThreads = maxSpareThreads; //设置最小多余线程数为最大多余线程数.
  174.       }
  175.       if (minSpareThreads <= 0) { //如果最小多余线程数小于0
  176.         if (1 == maxSpareThreads) {
  177.           minSpareThreads = 1; //如最大线程数为1的情况下,则设置最小多余线程数为1.
  178.         }
  179.         else {
  180.           minSpareThreads = maxSpareThreads / 2; //否则设置最小多余线程数为最大多余线程数的一半.
  181.         }
  182.       }
  183.     }

  184.     /**
  185.      * 打开指定数量的空闲线程队列
  186.      * @param toOpen int
  187.      */
  188.     protected void openThreads(int toOpen) { //toOpen=minSpareThreads
  189.       if (toOpen > maxThreads) {
  190.         toOpen = maxThreads;
  191.       }
  192.       if (0 == currentThreadCount) { //如果当前线程池中的线程数量为0
  193.         pool = new Vector(toOpen); //创建一个有minSpareThreads数量的Vector
  194.       }
  195.       //因第二次增加时对第一次增加的线程不能重复增加.所要从currentThreadCount开始.
  196.       for (int i = currentThreadCount; i < toOpen; i++) { //先增加minSparethreads数量的线程.
  197.         pool.addElement(new ControlRunnable(this)); //Runnable实例对象,可用于创建线程
  198.       }
  199.       currentThreadCount = toOpen;
  200.     }

  201.     /**
  202.      * 监视线程,用于监听当前空闲线程是否大于最大多余线程数量,如存在则关闭多余的空闲线程.
  203.      */
  204.     class MonitorRunnable
  205.         implements Runnable {
  206.       ThreadPool p;
  207.       Thread t;
  208.       boolean shouldTerminate;

  209.       /**
  210.        * construct
  211.        * @param p ThreadPool
  212.        */
  213.       MonitorRunnable(ThreadPool p) {
  214.         shouldTerminate = false;
  215.         this.p = p;
  216.         t = new Thread(this);
  217.         t.start();
  218.       }

  219.       public void run() {
  220.         while (true) {
  221.           try {
  222.             synchronized (this) {
  223.               this.wait(WORK_WAIT_TIMEOUT);
  224.             }
  225.             if (shouldTerminate) { //如果结束
  226.               break;
  227.             }
  228.             p.checkSpareControllers(); //检查是否有多余线程.
  229.           }
  230.           catch (Throwable t) {
  231.             t.printStackTrace();
  232.           }
  233.         }
  234.       }

  235.       public void stop() {
  236.         this.terminate();
  237.       }

  238.       public synchronized void terminate() {
  239.         shouldTerminate = true;
  240.         this.notifyAll();
  241.       }
  242.     }
  243. }

你可能感兴趣的:(java,socket,职场,休闲)