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是无界的。