创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。
// 源码
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
通过源码不难发现,通过newFixedThreadPool
创建线程池的核心线程数和最大线程数是相等的,因此该线程池中的工作线程数将始终是固定的。任务队列为LinkedBlockingQueue
(无边界),所以理论上提交至线程池的任务始终都会被执行。
创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。
// 源码
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()));
}
通过源码不难发现,通过newSingleThreadExecutor
创建线程池的核心线程数和最大线程数都是1,因此该线程池中的始终只有1工作线程。任务队列为LinkedBlockingQueue
(无边界),所以理论上提交至线程池的任务始终都会被执行。
创建一个可缓存线程池
// 源码
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}
newCachedThreadPool
根据需要创建新线程,但会重用以前构造的可用线程。当有新任务到来,则插入到SynchronousQueue
队列中,由于SynchronousQueue是同步队列,因此会在池中寻找可用线程来执行,若有可用线程则执行,若没有可用线程则创建一个线程来执行该任务;若池中线程空闲时间超过指定大小,则该线程会被销毁。
创建一个可定期或者延时执行任务的定长线程池,支持定时及周期性任务执行
// 源码
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
return new ScheduledThreadPoolExecutor(corePoolSize);
}
// ScheduledThreadPoolExecutor构造函数
public ScheduledThreadPoolExecutor(int corePoolSize) {
super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
new DelayedWorkQueue());
}
创建一个固定大小的线程池,线程池内线程存活时间无限制,线程池可以支持定时及周期性任务执行,如果所有线程均处于繁忙状态,对于新任务会进入DelayedWorkQueue队列中,这是一种按照超时时间排序的队列结构。
在实际开发中线程池不允许使用Executors
去创建,而是通过 ThreadPoolExecutor 的方式,这样
的处理方式可以更加明确线程池的运行规则,规避资源耗尽的风险。
1)newFixedThreadPool 和 newSingleThreadExecutor:
允许的请求队列长度为 Integer.MAX_VALUE,可能会堆积大量的请求,从而导致 OOM。
2)newCachedThreadPool 和 newScheduledThreadPool:
允许的创建线程数量为 Integer.MAX_VALUE,可能会创建大量的线程,从而导致 OOM。