Java中的線程池ExecutorService

來源:互聯網
上載者:User

標籤:nan   err   nano   查看   work   好的   cat   rac   block   

樣本

 

    import java.util.concurrent.ExecutorService;      import java.util.concurrent.Executors;      import java.util.concurrent.ScheduledExecutorService;            public class Ch09_Executor {                   private static void run(ExecutorService threadPool) {          for(int i = 1; i < 5; i++) {                    final int taskID = i;                    threadPool.execute(new Runnable() {                        @Override              public void run() {                            for(int i = 1; i < 5; i++) {                                try {                                    Thread.sleep(20);// 為了測試出效果,讓每次任務執行都需要一定時間                                } catch (InterruptedException e) {                                    e.printStackTrace();                                }                                System.out.println("第" + taskID + "次任務的第" + i + "次執行");                            }                        }                    });                }                threadPool.shutdown();// 任務執行完畢,關閉線程池           }                    public static void main(String[] args) {              // 建立可以容納3個線程的線程池              ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);                            // 線程池的大小會根據執行的任務數動態分配              ExecutorService cachedThreadPool = Executors.newCachedThreadPool();                                // 建立單個線程的線程池,如果當前線程在執行任務時突然中斷,則會建立一個新的線程替代它繼續執行任務                ExecutorService singleThreadPool = Executors.newSingleThreadExecutor();                            // 效果類似於Timer定時器              ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(3);                            run(fixedThreadPool);      //      run(cachedThreadPool);      //      run(singleThreadPool);      //      run(scheduledThreadPool);          }            }  

 


CachedThreadPool

 

CachedThreadPool會建立一個緩衝區,將初始化的線程緩衝起來。會終止並且從緩衝中移除已有60秒未被使用的線程。

 

如果線程有可用的,就使用之前建立好的線程,

 

如果線程沒有可用的,就新建立線程。

 


 

  • 重用:緩衝型池子,先查看池中有沒有以前建立的線程,如果有,就reuse;如果沒有,就建一個新的線程加入池中
  • 使用情境:緩衝型池子通常用於執行一些生存期很短的非同步型任務,因此在一些連線導向的daemon型SERVER中用得不多。
  • 逾時:能reuse的線程,必須是timeout IDLE內的池中線程,預設timeout是60s,超過這個IDLE時間長度,線程執行個體將被終止及移出池。
  • 結束:注意,放入CachedThreadPool的線程不必擔心其結束,超過TIMEOUT不活動,其會自動被終止。

 

    // 線程池的大小會根據執行的任務數動態分配      ExecutorService cachedThreadPool = Executors.newCachedThreadPool();            public static ExecutorService newCachedThreadPool() {          return new ThreadPoolExecutor(0,                 //core pool size                                        Integer.MAX_VALUE, //maximum pool size                                        60L,               //keep alive time                                        TimeUnit.SECONDS,                                        new SynchronousQueue<Runnable>());      }  執行結果:    第1次任務的第1次執行      第4次任務的第1次執行      第3次任務的第1次執行      第2次任務的第1次執行      第3次任務的第2次執行      第4次任務的第2次執行      第2次任務的第2次執行      第1次任務的第2次執行      第2次任務的第3次執行      第4次任務的第3次執行      第3次任務的第3次執行      第1次任務的第3次執行      第2次任務的第4次執行      第1次任務的第4次執行      第3次任務的第4次執行      第4次任務的第4次執行  4個任務是交替執行的。FixedThreadPool在FixedThreadPool中,有一個固定大小的池。如果當前需要執行的任務超過池大小,那麼多出的任務處於等待狀態,直到有空閑下來的線程執行任務,如果當前需要執行的任務小於池大小,閒置線程也不會去銷毀。    重用:fixedThreadPool與cacheThreadPool差不多,也是能reuse就用,但不能隨時建新的線程    固定數目:其獨特之處在於,任意時間點,最多隻能有固定數目的活動線程存在,此時如果有新的線程要建立,只能放在另外的隊列中等待,直到當前的線程中某個線程終止直接被移出池子    逾時:和cacheThreadPool不同,FixedThreadPool沒有IDLE機制(可能也有,但既然文檔沒提,肯定非常長,類似依賴上層的TCP或UDP IDLE機制之類的),    使用情境:所以FixedThreadPool多數針對一些很穩定很固定的正規並發線程,多用於伺服器    源碼分析:從方法的原始碼看,cache池和fixed 池調用的是同一個底層池,只不過參數不同:    fixed池線程數固定,並且是0秒IDLE(無IDLE)    cache池線程數支援0-Integer.MAX_VALUE(顯然完全沒考慮主機的資源承受能力),60秒IDLE    // 建立可以容納3個線程的線程池      ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);            public static ExecutorService newFixedThreadPool(int nThreads) {              return new ThreadPoolExecutor(nThreads, //core pool size                                            nThreads, //maximum pool size                                            0L,       //keep alive time                                            TimeUnit.MILLISECONDS,                                            new LinkedBlockingQueue<Runnable>());      }  

 

執行結果:

  1. 第1次任務的第1次執行  
  2. 第3次任務的第1次執行  
  3. 第2次任務的第1次執行  
  4. 第3次任務的第2次執行  
  5. 第2次任務的第2次執行  
  6. 第1次任務的第2次執行  
  7. 第3次任務的第3次執行  
  8. 第1次任務的第3次執行  
  9. 第2次任務的第3次執行  
  10. 第3次任務的第4次執行  
  11. 第1次任務的第4次執行  
  12. 第2次任務的第4次執行  
  13. 第4次任務的第1次執行  
  14. 第4次任務的第2次執行  
  15. 第4次任務的第3次執行  
  16. 第4次任務的第4次執行  


建立了一個固定大小的線程池,容量為3,然後迴圈執行了4個任務。由輸出結果可以看到,前3個任務首先執行完,然後空閑下來的線程去執行第4個任務。


 

SingleThreadExecutor

 

SingleThreadExecutor得到的是一個單個的線程,這個線程會保證你的任務執行完成。

 

如果當前線程意外終止,會建立一個新線程繼續執行任務,這和我們直接建立線程不同,也和newFixedThreadPool(1)不同。

 

    // 建立單個線程的線程池,如果當前線程在執行任務時突然中斷,則會建立一個新的線程替代它繼續執行任務        ExecutorService singleThreadPool = Executors.newSingleThreadExecutor();            public static ExecutorService newSingleThreadExecutor() {              return new FinalizableDelegatedExecutorService                  (new ThreadPoolExecutor(1,  //core pool size                                          1,  //maximum pool size                                          0L, //keep alive time                                          TimeUnit.MILLISECONDS,                                          new LinkedBlockingQueue<Runnable>()));      }  

 


執行結果:

 

  1. 第1次任務的第1次執行  
  2. 第1次任務的第2次執行  
  3. 第1次任務的第3次執行  
  4. 第1次任務的第4次執行  
  5. 第2次任務的第1次執行  
  6. 第2次任務的第2次執行  
  7. 第2次任務的第3次執行  
  8. 第2次任務的第4次執行  
  9. 第3次任務的第1次執行  
  10. 第3次任務的第2次執行  
  11. 第3次任務的第3次執行  
  12. 第3次任務的第4次執行  
  13. 第4次任務的第1次執行  
  14. 第4次任務的第2次執行  
  15. 第4次任務的第3次執行  
  16. 第4次任務的第4次執行  


4個任務是順序執行的
。


ScheduledThreadPool

 

ScheduledThreadPool是一個固定大小的線程池,與FixedThreadPool類似,執行的任務是定時執行。

 

  1. // 效果類似於Timer定時器  
ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(3);    public ScheduledThreadPoolExecutor(int corePoolSize) {          super(corePoolSize,      //core pool size                Integer.MAX_VALUE, //maximum pool size                0,                 //keep alive time                TimeUnit.NANOSECONDS,                new DelayedWorkQueue());  }  

 


執行結果:

  1. 第1次任務的第1次執行  
  2. 第2次任務的第1次執行  
  3. 第3次任務的第1次執行  
  4. 第2次任務的第2次執行  
  5. 第1次任務的第2次執行  
  6. 第3次任務的第2次執行  
  7. 第2次任務的第3次執行  
  8. 第1次任務的第3次執行  
  9. 第3次任務的第3次執行  
  10. 第2次任務的第4次執行  
  11. 第1次任務的第4次執行  
  12. 第3次任務的第4次執行  
  13. 第4次任務的第1次執行  
  14. 第4次任務的第2次執行  
  15. 第4次任務的第3次執行  
  16. 第4次任務的第4次執行  


——與FixedThreadPool的區別?

線程池

Java中的線程池ExecutorService

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.