<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)  編輯  收藏

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


    網站導航:
     
    主站蜘蛛池模板: 日产亚洲一区二区三区| 久久亚洲AV成人无码软件| 你懂得的在线观看免费视频| 亚洲AV无码一区二区二三区入口| 最近中文字幕2019高清免费| 亚洲AV无码国产一区二区三区| JLZZJLZZ亚洲乱熟无码| 114一级毛片免费| 特级做a爰片毛片免费看| 久久综合亚洲色HEZYO社区| 国产美女a做受大片免费| 男人的天堂网免费网站| 亚洲国产欧美国产综合一区| 久久亚洲国产午夜精品理论片 | 午夜免费啪视频在线观看| 亚洲成a人片在线看| 亚洲日韩中文无码久久| 最近中文字幕无吗高清免费视频| 久久国产精品免费一区二区三区 | mm1313亚洲国产精品无码试看| 亚洲精品国产精品乱码视色| 女人18毛片水真多免费播放| 毛片在线播放免费观看| 麻豆一区二区三区蜜桃免费| 91亚洲国产成人久久精品网站| 亚洲第一区在线观看| 久久久久久99av无码免费网站| 中文字幕无码日韩专区免费| 男男gay做爽爽的视频免费| 亚洲人成综合在线播放| 国产亚洲美女精品久久久2020 | 亚洲另类春色校园小说| 亚洲人成网77777色在线播放| 热99re久久精品精品免费| 99久热只有精品视频免费看| 精选影视免费在线 | 久久亚洲中文无码咪咪爱| 亚洲婷婷第一狠人综合精品| 亚洲午夜久久影院| 亚洲AV无码一区二区乱子伦 | 亚洲乱妇老熟女爽到高潮的片|