ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler)
參數說明:java
-corePoolSize 線程池中所保存的核心線程數。線程池啓動後默認是空的,只有任務來臨時纔會建立線程以處理請求。prestartAllCoreThreads方法能夠在線程池啓動後即啓動全部核心線程以等待任務。數組
-maximumPoolSize 線程池容許建立的最大線程數。當workQueue使用無界隊列時(如:LinkedBlockingQueue),則此參數無效。less
-keepAliveTime 當前線程池線程總數大於核心線程數時,終止多餘的空閒線程的時間。ide
-unit keepAliveTime參數的時間單位。this
-workQueue 工做隊列,若是當前線程池達到核心線程數時(corePoolSize),且當前全部線程都處於活動狀態,則將新加入的任務放到此隊列中。下面僅列幾個經常使用的:spa
-handler 拒絕策略,當線程池與workQueue隊列都滿了的狀況下,對新加任務採起的策略。線程
RejectedExecutionException
異常。默認值。package com.clzhang.sample.thread; import java.util.concurrent.BlockingQueue; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.PriorityBlockingQueue; import java.util.concurrent.SynchronousQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; public class ThreadPoolTest3 { static class MyThread implements Runnable { private String name; public MyThread(String name) { this.name = name; } @Override public void run() { // 作點事情 try { Thread.sleep(1000); System.out.println(name + " finished job!") ; } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args) { // 建立線程池,爲了更好的明白運行流程,增長了一些額外的代碼 // BlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(2); BlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>(); // BlockingQueue<Runnable> queue = new PriorityBlockingQueue<Runnable>(); // BlockingQueue<Runnable> queue = new SynchronousQueue<Runnable>();
// AbortPolicy/CallerRunsPolicy/DiscardOldestPolicy/DiscardPolicy ThreadPoolExecutor threadPool = new ThreadPoolExecutor(2, 4, 5, TimeUnit.SECONDS, queue, new ThreadPoolExecutor.AbortPolicy()); // 向線程池裏面扔任務 for (int i = 0; i < 10; i++) { System.out.println("當前線程池大小[" + threadPool.getPoolSize() + "],當前隊列大小[" + queue.size() + "]"); threadPool.execute(new MyThread("Thread" + i)); } // 關閉線程池 threadPool.shutdown(); } }
輸出(採用LinkedBlockingQueue隊列):rest
當前線程池大小[0],當前隊列大小[0]
當前線程池大小[1],當前隊列大小[0]
當前線程池大小[2],當前隊列大小[0]
當前線程池大小[2],當前隊列大小[1]
當前線程池大小[2],當前隊列大小[2]
當前線程池大小[2],當前隊列大小[3]
當前線程池大小[2],當前隊列大小[4]
當前線程池大小[2],當前隊列大小[5]
當前線程池大小[2],當前隊列大小[6]
當前線程池大小[2],當前隊列大小[7]
Thread1 finished job!
Thread0 finished job!
Thread3 finished job!
Thread2 finished job!
Thread4 finished job!
Thread5 finished job!
Thread6 finished job!
Thread7 finished job!
Thread8 finished job!
Thread9 finished job!code
Executors.newSingleThreadExecutor()blog
public static ExecutorService newSingleThreadExecutor() { return new FinalizableDelegatedExecutorService (new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>())); }
Executors.newFixedThreadPool()
public static ExecutorService newFixedThreadPool(int nThreads) { return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); }
Executors.newCachedThreadPool()
public static ExecutorService newCachedThreadPool() { return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>()); }
經過上述代碼能夠發現,用Executors靜態工廠生成的幾種經常使用線程池,均可以向裏面插入n多任務:要麼workQueue是無界的,要麼maximumPoolSize是無界的。