Java 併發包中的幾種ExecutorService

1.CachedThreadPool

    CachedThreadPool首先會按照需要創建足夠多的線程來執行任務(Task)。隨着程序執行的過程,有的線程執行完了任務,可以被重新循環使用時,纔不再創建新的線程來執行任務。我們採用《Thinking In Java》中的例子來分析。

    首先,任務定義如下(實現了Runnable接口,並且複寫了run方法):

 

Java代碼  收藏代碼
  1. package net.jerryblog.concurrent;   
  2. public class LiftOff implements Runnable{   
  3.     protected int countDown = 10//Default   
  4.     private static int taskCount = 0;   
  5.     private final int id = taskCount++;    
  6.     public LiftOff() {}   
  7.     public LiftOff(int countDown) {   
  8.         this.countDown = countDown;   
  9.     }   
  10.     public String status() {   
  11.         return "#" + id + "(" +   
  12.             (countDown > 0 ? countDown : "LiftOff!") + ") ";   
  13.     }   
  14.     @Override   
  15.     public void run() {   
  16.         while(countDown-- > 0) {   
  17.             System.out.print(status());   
  18.             Thread.yield();   
  19.         }   
  20.            
  21.     }      
  22. }   

    採用CachedThreadPool方式執行編寫的客戶端程序如下: 

 

Java代碼  收藏代碼
  1. package net.jerryblog.concurrent;   
  2. import java.util.concurrent.ExecutorService;   
  3. import java.util.concurrent.Executors;   
  4. public class CachedThreadPool {   
  5.     public static void main(String[] args) {   
  6.         ExecutorService exec = Executors.newCachedThreadPool();   
  7.         for(int i = 0; i < 10; i++) {   
  8.             exec.execute(new LiftOff());   
  9.         }   
  10.         exec.shutdown();       
  11.     }   
  12. }   

 上面的程序中,有10個任務,採用CachedThreadPool模式,exec沒遇到一個LiftOff的對象(Task),就會創建一個線程來處理任務。現在假設遇到到第4個任務時,之前用於處理第一個任務的線程已經執行完成任務了,那麼不會創建新的線程來處理任務,而是使用之前處理第一個任務的線程來處理這第4個任務。接着如果遇到第5個任務時,前面那些任務都還沒有執行完,那麼就會又新創建線程來執行第5個任務。否則,使用之前執行完任務的線程來處理新的任務。

2.FixedThreadPool

     FixedThreadPool模式會使用一個優先固定數目的線程來處理若干數目的任務。規定數目的線程處理所有任務,一旦有線程處理完了任務就會被用來處理新的任務(如果有的話)。這種模式與上面的CachedThreadPool是不同的,CachedThreadPool模式下處理一定數量的任務的線程數目是不確定的。而FixedThreadPool模式下最多 的線程數目是一定的。

    採用FixedThreadPool模式編寫客戶端程序如下:

 

Java代碼  收藏代碼
  1. package net.jerryblog.concurrent;   
  2. import java.util.concurrent.ExecutorService;   
  3. import java.util.concurrent.Executors;   
  4. public class FixedThreadPool {   
  5.     public static void main(String[] args) {  
  6.         //三個線程來執行五個任務   
  7.         ExecutorService exec = Executors.newFixedThreadPool(3);      
  8.         for(int i = 0; i < 5; i++) {   
  9.             exec.execute(new LiftOff());   
  10.         }   
  11.         exec.shutdown();   
  12.     }   
  13. }   

3.SingleThreadExecutor模式

    SingleThreadExecutor模式只會創建一個線程。它和FixedThreadPool比較類似,不過線程數是一個。如果多個任務被提交給SingleThreadExecutor的話,那麼這些任務會被保存在一個隊列中,並且會按照任務提交的順序,一個先執行完成再執行另外一個線程。

    SingleThreadExecutor模式可以保證只有一個任務會被執行。這種特點可以被用來處理共享資源的問題而不需要考慮同步的問題。

    SingleThreadExecutor模式編寫的客戶端程序如下: 

 

Java代碼  收藏代碼
  1. package net.jerryblog.concurrent;   
  2. import java.util.concurrent.ExecutorService;   
  3. import java.util.concurrent.Executors;   
  4. public class SingleThreadExecutor {   
  5.     public static void main(String[] args) {   
  6.         ExecutorService exec = Executors.newSingleThreadExecutor();   
  7.         for (int i = 0; i < 2; i++) {   
  8.             exec.execute(new LiftOff());   
  9.         }   
  10.     }   
  11. }   

    這種模式下執行的結果如下:

Java代碼  收藏代碼
  1. #0(9) #0(8) #0(7) #0(6) #0(5) #0(4) #0(3) #0(2) #0(1) #0(LiftOff!)  
  2. #1(9) #1(8) #1(7) #1(6) #1(5) #1(4) #1(3) #1(2) #1(1) #1(LiftOff!)   

    第一個任務執行完了之後纔開始執行第二個任務。   

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