Java并发包中的Executors类提供4种创建线程池的方法

newFixedThreadPool

创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。

  1. // 源码
  2. public static ExecutorService newFixedThreadPool(int nThreads) {
  3. return new ThreadPoolExecutor(nThreads, nThreads,
  4. 0L, TimeUnit.MILLISECONDS,
  5. new LinkedBlockingQueue<Runnable>());
  6. }

通过源码不难发现,通过newFixedThreadPool创建线程池的核心线程数和最大线程数是相等的,因此该线程池中的工作线程数将始终是固定的。任务队列为LinkedBlockingQueue(无边界),所以理论上提交至线程池的任务始终都会被执行。

newSingleThreadExecutor

创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。

  1. // 源码
  2. public static ExecutorService newSingleThreadExecutor() {
  3. return new FinalizableDelegatedExecutorService
  4. (new ThreadPoolExecutor(1, 1,
  5. 0L, TimeUnit.MILLISECONDS,
  6. new LinkedBlockingQueue<Runnable>()));
  7. }

通过源码不难发现,通过newSingleThreadExecutor创建线程池的核心线程数和最大线程数都是1,因此该线程池中的始终只有1工作线程。任务队列为LinkedBlockingQueue(无边界),所以理论上提交至线程池的任务始终都会被执行。

newCachedThreadPool

创建一个可缓存线程池

  1. // 源码
  2. public static ExecutorService newCachedThreadPool() {
  3. return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
  4. 60L, TimeUnit.SECONDS,
  5. new SynchronousQueue<Runnable>());
  6. }

newCachedThreadPool根据需要创建新线程,但会重用以前构造的可用线程。当有新任务到来,则插入到SynchronousQueue队列中,由于SynchronousQueue是同步队列,因此会在池中寻找可用线程来执行,若有可用线程则执行,若没有可用线程则创建一个线程来执行该任务;若池中线程空闲时间超过指定大小,则该线程会被销毁。

newScheduledThreadPool

创建一个可定期或者延时执行任务的定长线程池,支持定时及周期性任务执行

  1. // 源码
  2. public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
  3. return new ScheduledThreadPoolExecutor(corePoolSize);
  4. }
  5. // ScheduledThreadPoolExecutor构造函数
  6. public ScheduledThreadPoolExecutor(int corePoolSize) {
  7. super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
  8. new DelayedWorkQueue());
  9. }

创建一个固定大小的线程池,线程池内线程存活时间无限制,线程池可以支持定时及周期性任务执行,如果所有线程均处于繁忙状态,对于新任务会进入DelayedWorkQueue队列中,这是一种按照超时时间排序的队列结构。

注意风险

在实际开发中线程池不允许使用Executors去创建,而是通过 ThreadPoolExecutor 的方式,这样
的处理方式可以更加明确线程池的运行规则,规避资源耗尽的风险。

说明:Executors 返回的线程池对象的弊端如下:

1)newFixedThreadPool 和 newSingleThreadExecutor:
允许的请求队列长度为 Integer.MAX_VALUE,可能会堆积大量的请求,从而导致 OOM。
2)newCachedThreadPool 和 newScheduledThreadPool:
允许的创建线程数量为 Integer.MAX_VALUE,可能会创建大量的线程,从而导致 OOM。


© 2020-2025 www.chenhuazhan.com All Rights Reserved 备案号:桂ICP备17004487号-1 粤公网安备44030002005146