線程池管理

  1. package test.dao;  
  2.   
  3. import java.util.LinkedList;  
  4.   
  5. import org.apache.log4j.Logger;  
  6.   
  7.   
  8. /** 
  9.  *  
  10.  * @author Andy.xiaomeng 
  11.  * 
  12.  */  
  13. public class ThreadPool {  
  14.   
  15.     private static final Logger Log = Logger.getLogger(ThreadPool.class);  
  16.       
  17.     private ThreadWorker[] worker;  
  18.     private LinkedList<Runnable> queue;  
  19.     private int poolSize;  
  20.       
  21.     public int getPoolSize(){  
  22.         return poolSize;  
  23.     }  
  24.       
  25.     //initialize thread pool with pool size  
  26.     public ThreadPool(int size){  
  27.           
  28.         poolSize = size;  
  29.         worker = new ThreadWorker[size];  
  30.         queue = new LinkedList<Runnable>();  
  31.           
  32.         for (int i = 0; i < size; i++) {  
  33.             worker[i] = new ThreadWorker();   
  34.             worker[i].start();  
  35.         }  
  36.           
  37.     }  
  38.       
  39.       
  40.       
  41.     //add work queue  
  42.     public void addQueue(Runnable runnable){  
  43.         synchronized(queue) {  
  44.             queue.add(runnable);  
  45.             queue.notify();  
  46.         }  
  47.     }  
  48.       
  49.     //worker do work  
  50.     private class ThreadWorker extends Thread{  
  51.           
  52.         public void run(){  
  53.               
  54.             //runnable  
  55.             Runnable runable;  
  56.               
  57.             while (true) {  
  58.                   
  59.                 synchronized(queue) {  
  60.                     while (queue.isEmpty()) {  
  61.                         try  
  62.                         {  
  63.                             //is work queue is empty, wait  
  64.                             queue.wait();  
  65.                         }  
  66.                         catch (Exception e)  
  67.                         {  
  68.                             Log.error("pool queue wait", e);  
  69.                         }  
  70.                     }  
  71.                       
  72.                     //get work in queue, and remove it   
  73.                     runable = queue.removeFirst();  
  74.                 }  
  75.   
  76.                 try {  
  77.                     //do work  
  78.                     runable.run();  
  79.                 }  
  80.                 catch (RuntimeException e) {  
  81.                     Log.error("unknow runtime exception", e);  
  82.                 }  
  83.                   
  84.             }  
  85.         }  
  86.           
  87.     }  
  88.       
  89.     public static void main(String[] args) {  
  90.           
  91.         //test  
  92.         ThreadPool pool = new ThreadPool(111);  
  93.           
  94.           
  95.         //  
  96.         class SSS implements Runnable{  
  97.             private String abc;  
  98.             public void setAbc(String aaaaa){  
  99.                 this.abc = aaaaa;  
  100.             }  
  101.               
  102.             public void run() {  
  103.                 System.out.println("do runnable " + abc);  
  104.             }  
  105.         }  
  106.           
  107.         for (int i = 0; i < 200; i++) {  
  108.             SSS a = new SSS();  
  109.             a.setAbc(String.valueOf(i + 1));  
  110.             pool.addQueue(a);  
  111.         }  
  112.           
  113.     }  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章