一、通过Executors类提供的静态方法创建
1. newFixedThreadPool
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
}
public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {
return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), threadFactory);
}
- 生成一个固定大小的线程池,当有新任务提交而没有空闲线程时,将任务置于工作队列,该队列是一个无界队列
- 当线程池中的一个线程因执行失败而被关闭后,会重新创建一个新的线程
- 线程池中的线程会一直存活直到线程池被关闭。
2. newSingleThreadExecutor
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()));
}
public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory) {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), threadFactory));
}
- 在该线程池中自始至终只有一个线程,当有新任务提交而该线程不空闲时,将新任务置于任务队列,队列是一个无界队列
- 当该线程因执行任务失败而被关闭会会重新创建一个线程
- 该线程池保证任务按序执行
- 与newFixedThreadPool的区别是,该线程池后期不能够修改配置即线程池的大小,而nexFixedThreadPool可以
3. newCachedThreadPool
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());
}
public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), threadFactory);
}
- 仅创建所需数目的线程,当有新任务提交时,从线程池中取出一个空闲线程执行,若没有空闲线程则创建一个新线程
- 该线程池不会限制线程池的大小,可以创建JVM允许的最大创建数
- 当线程的空闲时间超过60秒时,该线程会被关闭,减少资源的消耗
4. newScheduledThreadPool
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
return new ScheduledThreadPoolExecutor(corePoolSize);
}
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize, ThreadFactory threadFactory) {
return new ScheduledThreadPoolExecutor(corePoolSize, threadFactory);
}
二、通过ThreadPoolExecutor的构造方法
public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) {
if (corePoolSize < 0 ||
maximumPoolSize <= 0 ||
maximumPoolSize < corePoolSize ||
keepAliveTime < 0)
throw new IllegalArgumentException();
if (workQueue == null || threadFactory == null || handler == null)
throw new NullPointerException();
this.acc = System.getSecurityManager() == null ?
null :
AccessController.getContext();
this.corePoolSize = corePoolSize;
this.maximumPoolSize = maximumPoolSize;
this.workQueue = workQueue;
this.keepAliveTime = unit.toNanos(keepAliveTime);
this.threadFactory = threadFactory;
this.handler = handler;
}
- corePoolSize:核心线程数,即使没有任务处理,核心线程也会一直存活。若当前线程数小于核心线程数时,即使有空闲线程,也会创建一个新的线程执行任务。
- maximumPoolSize:最大线程数,即线程池允许创建的最大线程数。
- keepAliveTime:空闲线程的保活时间,当线程空闲时间达到该值时,该线程会退出,直至线程数减至核心线程数。
- unit:keepAliveTime的单位。
- workQueue:工作队列,存放待提交的任务。
- threadFactory:创建线程的工厂。
- handler:当线程池已满而又有任务提交时采用的策略,具体Java 线程池的拒绝策略 RejectedExecutorHandler