http://www.cnblogs.com/MOBIN/p/5436482.html
摘要: Eexecutor作為靈活且強大的非同步執行架構,其支援多種不同類型的任務執行策略,提供了一種標準的方法將任務的提交過程和執行過程解耦開發,基於生產者-消費者模式,其提交任務的線程相當於生產者,執行任務的線程相當於消費者,並用Runnable來表示任務,Executor的實現還提供了對生命週期的支援,以及統計資訊收集,應用程式管理機制和效能監控等機制。 1.Exexctor簡介 Executor的UML圖:(常用的幾個介面和子類)
Executor:一個介面,其定義了一個接收Runnable對象的方法executor,其方法簽名為executor(Runnable command), ExecutorService: 是一個比Executor使用更廣泛的子類介面,其提供了生命週期管理的方法,以及可跟蹤一個或多個非同步任務執行狀況返回Future的方法 AbstractExecutorService:ExecutorService執行方法的預設實現 ScheduledExecutorService:一個可定時調度任務的介面 ScheduledThreadPoolExecutor:ScheduledExecutorService的實現,一個可定時調度任務的線程池 ThreadPoolExecutor:線程池,可以通過調用Executors以下靜態Factory 方法來建立線程池 並返回一個ExecutorService對象: 2.ThreadPoolExecutor建構函式的各個參數說明 ThreadPoolExecutor方法簽名:
public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) //後兩個參數為選擇性參數
參數說明: corePoolSize:核心線程數,如果啟動並執行線程少於corePoolSize,則建立新線程來執行新任務,即使線程池中的其他線程是閒置 maximumPoolSize:最大線程數,可允許建立的線程數,corePoolSize和maximumPoolSize設定的邊界自動調整池大小: corePoolSize <啟動並執行線程數< maximumPoolSize:僅當隊列滿時才建立新線程 corePoolSize=啟動並執行線程數= maximumPoolSize:建立固定大小的線程池 keepAliveTime:如果線程數多於corePoolSize,則這些多餘的線程的空閑時間超過keepAliveTime時將被終止 unit:keepAliveTime參數的時間單位 workQueue:儲存任務的阻塞隊列,與線程池的大小有關: 當啟動並執行線程數少於corePoolSize時,在有新任務時直接建立新線程來執行任務而無需再進隊列 當啟動並執行線程數等於或多於corePoolSize,在有新任務添加時則選排入佇列,不直接建立線程 當隊列滿時,在有新任務時就建立新線程 threadFactory:使用ThreadFactory建立新線程,預設使用defaultThreadFactory建立線程 handle:定義處理被拒絕任務的策略,預設使用ThreadPoolExecutor.AbortPolicy,任務被拒絕時將拋出RejectExecutorException 3.Executors:提供了一系列靜態Factory 方法用於建立各種線程池 newFixedThreadPool:建立可重用且固定線程數的線程池,如果線程池中的所有線程都處於活動狀態,此時再提交任務就在隊列中等待,直到有可用線程;如果線程池中的某個線程由於異常而結束時,線程池就會再補充一條新線程。 方法簽名:
public static ExecutorService newFixedThreadPool(int nThreads) { return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, //使用一個基於FIFO排序的阻塞隊列,在所有corePoolSize線程都忙時新任務將在隊列中等待 new LinkedBlockingQueue<Runnable>());} newSingleThreadExecutor:建立一個單線程的Executor,如果該線程因為異常而結束就建立一條線程來繼續執行後續的任務 方法簽名:
public static ExecutorService newSingleThreadExecutor() { return new FinalizableDelegatedExecutorService //corePoolSize和maximumPoolSize都等於,表示固定線程池大小為1 (new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()));} newScheduledThreadPool:建立一個可順延強制或定期執行的線程池 方法簽名: 例1:(使用newScheduledThreadPool來類比心跳機制)
1 public class HeartBeat { 2 public static void main(String[] args) { 3 ScheduledExecutorService executor = Executors.newScheduledThreadPool(5); 4 Runnable task = new Runnable() { 5 public void run() { 6 System.out.println("HeartBeat........................."); 7 } 8 }; 9 executor.scheduleAtFixedRate(task,5,3, TimeUnit.SECONDS); //5秒後第一次執行,之後每隔3秒執行一次10 }11 } 輸出:
HeartBeat....................... //5秒後第一次輸出HeartBeat....................... //每隔3秒輸出一個
newCachedThreadPool:建立可快取的線程池,如果線程池中的線程在60秒未被使用就將被移除,在執行新的任務時,當線程池中有之前建立的可用線程就重 用可用線程,否則就建立一條線程 方法簽名:
public static ExecutorService newCachedThreadPool() { return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, //使用同步隊列,將任務直接提交給線程 new SynchronousQueue<Runnable>());} 例2: