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

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


    網站導航:
     
    主站蜘蛛池模板: 亚洲小说图片视频| 亚洲AV无码1区2区久久| 免费中文熟妇在线影片| 青苹果乐园免费高清在线| 亚洲精品不卡视频| 国产成人亚洲精品无码AV大片| 男男gay做爽爽的视频免费| 在线免费观看国产视频| AV在线播放日韩亚洲欧| 亚洲日韩乱码久久久久久| 最近中文字幕免费2019| 少妇亚洲免费精品| 亚洲自偷自拍另类图片二区| 免费看一级高潮毛片| 免费观看无遮挡www的视频| 国产免费牲交视频| 亚洲最大在线观看| 最近免费中文字幕4| 久久精品亚洲视频| 一级做受视频免费是看美女 | 国产h视频在线观看免费| 国产偷国产偷亚洲清高动态图 | 亚洲国产精品免费观看| 免费福利在线视频| 免费人妻av无码专区| 亚洲成aⅴ人片久青草影院按摩| 丝袜捆绑调教视频免费区| 毛片免费观看网址| 免费福利在线观看| 无码乱人伦一区二区亚洲一 | 国产一区视频在线免费观看 | 3d动漫精品啪啪一区二区免费| 亚洲开心婷婷中文字幕| 国产天堂亚洲国产碰碰| 国产亚洲美女精品久久久2020| 97国产在线公开免费观看| 亚洲久热无码av中文字幕| 无码一区二区三区AV免费| 日韩毛片一区视频免费| 亚洲精品福利视频| 中文字幕亚洲免费无线观看日本|