Java並發編程-Executor架構

來源:互聯網
上載者:User

標籤:大量   epo   單線程   down   rmi   while   nat   thread   nts   

本文轉自http://blog.csdn.net/chenchaofuck1/article/details/51606224 感謝作者

我們在傳統多線程編程建立線程時,常常是建立一些Runnable對象,然後建立對應的Thread對象執行它們,但是如果程式需要並發執行大量的任務時,需要為每個任務都建立一個Thread,進行管理,這將會影響程式的執行效率,並且建立線程過多將會使系統負載過重。

在JDK 1.5之後通過了一套Executor架構能夠解決這些問題,能夠分解任務的建立和執行過程。該架構套件括Executor,ExecutorService,Callable等基礎介面和Executors,ThreadPoolExecutor等實作類別。

 

建立線程池:

Executor架構的最核心的類是ThreadPoolExecutor,它是線程池的實作類別,建立ThreadPoolExecutor一般使用Executors原廠模式建立,Executors類提供了一系列Factory 方法用於創先線程池:

  • public static ExecutorService newFixedThreadPool(int nThreads)建立固定數目線程的線程池,表示最多建立nThreads個線程,如果傳入的任務數大於nThreads時不會建立新的線程,而是阻塞等待有空閑線程執行。

  • public static ExecutorService newCachedThreadPool()建立一個可快取的線程池,調用execute將重用以前構造的線程(如果線程可用)。如果現有線程沒有可用的,則建立一個新線程並添加到池中。終止並從緩衝中移除那些已有 60秒鐘未被使用的線程。

  • public static ExecutorService newSingleThreadExecutor()建立一個單線程的Executor。

  • public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) 建立一個支援定時及周期性的任務執行的線程池,多數情況下可用來替代Timer類。

 

常用方法:

  • shutDown():關閉執行器,在關閉前允許執行以前提交的任務執行器執行完。調用shutDown()後,再發送任務給Executor將會被拒絕,拋出RejectExecutionException異常。

  • shutdownNow() :立即關閉執行器,阻止等待任務啟動,並試圖停止當前正在執行的任務。返回等待執行的工作清單。

  • isShutdown():調用shutDown()後,返回true。

  • isTerminated():調用shutDown()後,並且執行器完成了關閉過程,返回true。

  • getPoolSize():擷取當前線程池的線程數量

  • getActiveCount():擷取線程池中活動線程的數量

  • getCompleteCount():擷取線程池中完成的任務數。

 

import java.util.concurrent.Executors; import java.util.concurrent.ThreadPoolExecutor; public class ExecutorTest { public static void main(String[] args){ ThreadPoolExecutor executor = (ThreadPoolExecutor)Executors.newCachedThreadPool(); for (int i = 0; i < 5; i++){ executor.execute(new task()); } executor.shutdown(); while(!executor.isTerminated()){ System.out.printf("Pool size:%d,Active count:%d,Completed Task:%d\n",executor.getPoolSize(),executor.getActiveCount(),executor.getCompletedTaskCount()); } } } class task implements Runnable{ public void run() { System.out.println(Thread.currentThread().getName() + " is called"); try { Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

 

 

結果:

pool-1-thread-2 is called 
pool-1-thread-4 is called 
pool-1-thread-5 is called 
pool-1-thread-3 is called 
pool-1-thread-1 is called 
Pool size:5,Active count:5,Completed Task:0 
Pool size:5,Active count:5,Completed Task:0 
Pool size:5,Active count:5,Completed Task:0 
Pool size:5,Active count:5,Completed Task:0

Java並發編程-Executor架構(轉)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.