<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-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 閱讀(729) 評論(0)  編輯  收藏

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


    網站導航:
     
    主站蜘蛛池模板: 亚洲精品少妇30p| 成人免费福利电影| 亚洲成人影院在线观看| 免费电影在线观看网站| 国产成人免费永久播放视频平台| 久久精品国产亚洲AV电影| 亚洲香蕉在线观看| 亚洲av成人中文无码专区| 久久精品免费大片国产大片| 国产高清在线精品免费软件| 中文字幕亚洲综合久久综合| 在线观看免费a∨网站| 亚洲精品无码成人| 国产美女无遮挡免费网站| 国产精品亚洲综合| 亚洲熟伦熟女新五十路熟妇 | 亚洲乱码中文字幕手机在线 | 亚洲成人午夜电影| www免费黄色网| 亚洲国产成人片在线观看| 久久伊人免费视频| 四虎国产精品免费视| 精品亚洲成在人线AV无码| 嫩草成人永久免费观看| 亚洲色图黄色小说| 国外成人免费高清激情视频| 全黄A免费一级毛片| 成年男女免费视频网站| 国产99久久亚洲综合精品 | 久久精品国产亚洲av麻豆小说 | 亚洲国产精品无码久久久| 成年人视频免费在线观看| 亚洲综合伊人久久综合| 久久w5ww成w人免费| 国产亚洲中文日本不卡二区| 亚洲阿v天堂在线2017免费| 久久久久久成人毛片免费看 | 午夜亚洲国产理论秋霞| 国产在线精品观看免费观看| 四虎影视在线永久免费观看| 嫩草影院在线播放www免费观看|