隨筆-199  評論-203  文章-11  trackbacks-0

          為什么要建立線程池?

           

          在多線程項目中,如果建立的線程過多,反而可能導致運行速度大大減慢,這是由于線程建立所花費的時間和資源都比較多。
          所以我們在多線程中必須很好地來管理線程, 在很好利用多線程能“同步工作”的好處之外,更有效地提高程序運行速度。

           

          線程池是什么?

           

          線程池是指具有固定數量的線程組成的一種組件。這些線程用來循環執行多個應用邏輯。

           

          怎么建立線程池?

           

          線程池主要包括4個部分,它們是:
          1. 線程管理
           

          主要是用來建立,啟動,銷毀工作線程和把工作任務加入工作線程。

           

          2. 工作線程
           

          它是真正的線程類,運行工作任務。

           

          3. 工作隊列
           

          它是用來封裝線程的容器。


          4. 工作任務
           

          它是實現應用邏輯的具體類。

           

           線程管理類:

          Java代碼 復制代碼
          1. <SPAN style="COLOR: #3366ff">import java.util.ArrayList;   
          2. import java.util.List;   
          3. import java.util.Queue;   
          4. import java.util.concurrent.ConcurrentLinkedQueue;   
          5.   
          6. /**  
          7.  * ThreadPoolManager.java  
          8.  *  
          9.  * Copyright (C)  2008 State Street Corporation. All Rights Reserved.  
          10.  *  
          11.  */  
          12.   
          13. /**  
          14.  * the thread pool manager, is responsible for starting and stopping the work thread.  
          15.  *   
          16.  * @author  e458487  
          17.  * @version 1.0  
          18.  */  
          19. public class ThreadPoolManager {   
          20.   
          21.     private static final int DEFAULT_POOL_SIZE = 4;   
          22.     private List<WorkThread> threadPool;   
          23.     private Queue<Task> taskQueue;   
          24.     private int poolSize;   
          25.        
          26.     public ThreadPoolManager() {   
          27.         this(DEFAULT_POOL_SIZE);   
          28.     }   
          29.        
          30.     public ThreadPoolManager(int poolSize) {   
          31.         if(poolSize <= 0) {   
          32.             this.poolSize = DEFAULT_POOL_SIZE;   
          33.         }else {   
          34.             this.poolSize = poolSize;   
          35.         }   
          36.         threadPool = new ArrayList<WorkThread>(this.poolSize);   
          37.         taskQueue = new ConcurrentLinkedQueue<Task>();   
          38.         startup();   
          39.     }   
          40.        
          41.     public void startup() {   
          42.         System.out.println("start work thread...");   
          43.         synchronized(taskQueue) {   
          44.             for(int i = 0; i < this.poolSize; i++) {   
          45.                 WorkThread workThread = new WorkThread(taskQueue);   
          46.                 threadPool.add(workThread);   
          47.                 workThread.start();   
          48.             }   
          49.         }   
          50.     }   
          51.        
          52.     public void shutdown() {   
          53.         System.out.println("shutdown work thread...");   
          54.         synchronized(taskQueue) {   
          55.             for(int i = 0; i < this.poolSize; i++) {   
          56.                 threadPool.get(i).shutdown();   
          57.             }              
          58.                
          59.             System.out.println("done...");   
          60.         }   
          61.     }   
          62.        
          63.     public void addTask(Task task) {   
          64.         synchronized(taskQueue) {   
          65.             taskQueue.add(task);   
          66.             taskQueue.notify();   
          67.         }   
          68.     }   
          69. }</SPAN>  

           

          工作線程類:

          Java代碼 復制代碼
          1. <SPAN style="COLOR: #3366ff">import java.util.Queue;   
          2.   
          3. /**  
          4.  * WorkThread.java  
          5.  *  
          6.  * Copyright (C)  2008 State Street Corporation. All Rights Reserved.  
          7.  *  
          8.  */  
          9.   
          10. /**  
          11.  * the work thread used pull the task of task queue, and execute it.  
          12.  *   
          13.  * @author  e458487  
          14.  * @version 1.0  
          15.  */  
          16. public class WorkThread extends Thread {   
          17.   
          18.     private boolean shutdown = false;   
          19.     private Queue<Task> queue;   
          20.        
          21.     public WorkThread(Queue<Task> queue) {   
          22.         this.queue = queue;   
          23.     }   
          24.        
          25.     public void run() {   
          26.         while(!shutdown) {   
          27.             try {   
          28.                 Thread.sleep(1000);   
          29.             } catch (InterruptedException e1) {   
          30.                 e1.printStackTrace();   
          31.             }   
          32.             System.out.println(Thread.currentThread() + " is running...");   
          33.             synchronized(queue) {   
          34.                 if(!queue.isEmpty()) {   
          35.                     Task task = queue.poll();   
          36.                     task.execute();   
          37.                 }else {   
          38.                     try {   
          39.                         queue.wait(1000);   
          40.                         System.out.println(Thread.currentThread() + " wait...");   
          41.                     }catch(InterruptedException e) {   
          42.                            
          43.                     }   
          44.                 }   
          45.             }   
          46.         }   
          47.     }   
          48.        
          49.     public void shutdown() {   
          50.         shutdown = true;   
          51.     }   
          52. }</SPAN>  

           

          工作任務接口:

           

          Java代碼 復制代碼
          1. <SPAN style="COLOR: #3366ff">/**  
          2.  * Task.java  
          3.  *  
          4.  * Copyright (C)  2008 State Street Corporation. All Rights Reserved.  
          5.  *  
          6.  */  
          7.   
          8. /**  
          9.  * The task want to execute.  
          10.  *   
          11.  * @author  e458487  
          12.  * @version 1.0  
          13.  */  
          14. public interface Task {   
          15.   
          16.     public void execute();   
          17. }</SPAN>  

           

          工作任務類:

          Java代碼 復制代碼
          1. <SPAN style="COLOR: #3366ff">/**  
          2.  * SimpleTask.java  
          3.  *  
          4.  * Copyright (C)  2008 State Street Corporation. All Rights Reserved.  
          5.  *  
          6.  */  
          7.   
          8. /**  
          9.  * @author  e458487  
          10.  * @version 1.0  
          11.  */  
          12. public class SimpleTask implements Task {   
          13.   
          14.     /* (non-Javadoc)  
          15.      * @see Task#execute()  
          16.      */  
          17.     public void execute() {   
          18.         System.out.println(Thread.currentThread());   
          19.     }   
          20.   
          21. }</SPAN>  

           

          線程池測試類:

          Java代碼 復制代碼
          1. <SPAN style="COLOR: #3366ff">/**  
          2.  * ThreadPoolDemo.java  
          3.  *  
          4.  * Copyright (C)  2008 State Street Corporation. All Rights Reserved.  
          5.  *  
          6.  */  
          7.   
          8. /**  
          9.  * @author  e458487  
          10.  * @version 1.0  
          11.  */  
          12. public class ThreadPoolDemo {   
          13.   
          14.     public static void main(String[] args) {   
          15.         ThreadPoolManager threadMg = new ThreadPoolManager();   
          16.            
          17.         for(int i = 0; i < 50; i++) {   
          18.             threadMg.addTask(new SimpleTask());   
          19.         }   
          20.         try {   
          21.             Thread.sleep(5000);   
          22.         } catch (InterruptedException e) {   
          23.             e.printStackTrace();   
          24.         }   
          25.         threadMg.shutdown();   
          26.     }      
          27. }</SPAN>  

          http://www.javaeye.com/topic/387566
          posted on 2009-05-15 19:27 Werther 閱讀(726) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 滦南县| 邢台市| 土默特左旗| 囊谦县| 揭东县| 兴仁县| 彭州市| 米易县| 多伦县| 岳西县| 万山特区| 宁蒗| 大同市| 星子县| 武威市| 乡宁县| 龙口市| 项城市| 哈巴河县| 镶黄旗| 晋中市| 呼图壁县| 同德县| 楚雄市| 沅陵县| 五华县| 珲春市| 屯门区| 巴南区| 石城县| 湘西| 玉溪市| 庐江县| 汉阴县| 寻乌县| 普兰县| 浮山县| 肥城市| 林周县| 岳普湖县| 蒙城县|