Executor框架能夠快速建立Java線程池。
Executors是Executor框架的基礎。全部的Executor和ExecutorService都實現Executor接口,其中ExecutorService是比較重要的接口,Executors提供了不少靜態方法能夠實現不少ThreadPool。經過ExecutorService的submit能夠分別將Callable和Runnable任務提交到線程中處理;經過shutdown()關閉全部線程,Callable返回的Future有結果,Runnable則沒有。
Executors類:java
public class Executors { public static ExecutorService newFixedThreadPool(int nThreads) { return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue()); } public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) { return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue(), threadFactory); } public static ExecutorService newSingleThreadExecutor() { return new Executors.FinalizableDelegatedExecutorService(new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue())); } public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory) { return new Executors.FinalizableDelegatedExecutorService(new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue(), threadFactory)); } public static ExecutorService newCachedThreadPool() { return new ThreadPoolExecutor(0, 2147483647, 60L, TimeUnit.SECONDS, new SynchronousQueue()); } public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) { return new ThreadPoolExecutor(0, 2147483647, 60L, TimeUnit.SECONDS, new SynchronousQueue(), threadFactory); } public static ScheduledExecutorService newSingleThreadScheduledExecutor() { return new Executors.DelegatedScheduledExecutorService(new ScheduledThreadPoolExecutor(1)); } public static ScheduledExecutorService newSingleThreadScheduledExecutor(ThreadFactory threadFactory) { return new Executors.DelegatedScheduledExecutorService(new ScheduledThreadPoolExecutor(1, threadFactory)); } public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) { return new ScheduledThreadPoolExecutor(corePoolSize); } public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize, ThreadFactory threadFactory) { return new ScheduledThreadPoolExecutor(corePoolSize, threadFactory); } }
主要方法:
1. newFixedThreadPool返回ThreadPoolExecutor,ThreadPoolExecutor建立固定數目的線程,當有任務submit時,若是沒有線程可執行時,則加入任務隊列。ThreadPoolExecutor剛開始不建立線程,當有任務提交時,若是poolSize<corePoolSize則建立線程,當poolSize==corePoolSize時,新增任務加入任務隊列,若是此時任務隊列滿了而且poolSize<maximumPoolSize,則建立線程處理,不然就拒絕任務,拒絕策略取決於RejectedExecutionHandler,Worker線程則一直在等待隊列getTask和run間循環;
2. newCachedThreadPool返回的也是ThreadPoolExecutor,區別是cache線程數大小沒有限制,有60秒的IDLE,若是超過60秒沒有任務則會被關閉;
3. newSingleThreadExecutor返回Executors.FinalizableDelegatedExecutorService,底層也是ThreadPoolExecutor,區別是線程是1~1,IDLE爲0秒;
4. newScheduledThreadPool返回ScheduledExecutorService,ScheduledExecutorService繼承ThreadPoolExecutor並實現了ScheduledExecutorService,ScheduledExecutorService封裝一個DelayedWorkQueue,Worker執行一次任務後add進去,另外一個Worker觸發take,take須要等待一個delay,重複循環下去。
5. newWorkStealingPool返回ForkJoinPool,ForkJoinPool分而治之,ForkJoinPool默認建立CPU數目建立並行等級,會動態的增長或關閉線程,最大線程數限制爲32767,空閒線程會主動爲別的線程分擔任務,從而使CPU處於飽滿狀態,咱們要使用ForkJoin框架,必須首先建立一個ForkJoinTask。它提供在任務中執行fork( )和join( )操做的機制,一般狀況下咱們不須要直接繼承ForkJoinTask類,而只須要繼承它的子類,Fork/Join框架提供瞭如下兩個子類:RecursiveAction用於沒有返回結果的任務,RecursiveTask 用於有返回結果的任務。ForkJoinTask須要經過ForkJoinPool來執行,任務分割出的子任務會添加到當前工做線程所維護的雙端隊列中,進入隊列的頭部。當一個工做線程的隊列裏暫時沒有任務時,它會隨機從其餘工做線程的隊列的尾部獲取一個任務。框架