为什么不推荐使用Executors线程池
Executors类提供了一些创建线程池的静态方法。但是阿里巴巴开发手册强制不允许使用Executors来创建线程池,下面根据源码分析为什么阿里巴巴开发手册不允许使用Executors类创建线程池。
newCachedThreadPool
/**
* 创建一个线程池,根据需要创建新线程,但在以前构造的线程可用时重用它们。
* 这些池通常会提高执行许多短期异步任务的程序的性能。对execute的调用将重用先前构造的线程(如果可用)。
* 如果没有可用的线程,将创建一个新线程并将其添加到池中。60秒内未使用的线程将被终止并从缓存中删除。
* 因此,空闲时间足够长的池不会消耗任何资源。
* @return the newly created thread pool
*/
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}
从源码中可以看出,核心线程为0,最大线程数为Integer.MAX_VALUE,队列为SynchronousQueue,该队列要求只有线程获取任务的话才能加入队列中。如果线程池的大小超过处理任务的线程,那么就会回收空闲线程。因为该线程池的最大线程数量为 Integer.MAX_VALUE,任务量过多时,可能会创建大量的线程,从而导致 OOM。
newFixedThreadPool(int nThreads)
/**
* 创建一个线程池,该线程池重用固定数量的线程,这些线程在共享无界队列上操作。
* 在任何时候,最多nThreads线程将是活动的处理任务。
* 如果在所有线程都处于活动状态时提交了额外的任务,它们将在队列中等待,直到有一个线程可用。
* 任何线程在关闭之前的执行过程中由于失败而终止,如需要执行后续任务,将会有一个新的线程取代它的位置。
* 在显式关闭池之前,池中的线程将一直存在。
* @param nThreads the number of threads in the pool
* @return the newly created thread pool
* @throws IllegalArgumentException if {@code nThreads <= 0}
*/
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
从源码中可以看出,该方法会创建一个固定大小的线程池。核心线程数和最大线程数一致,当接受一个任务后就创建一个线程直至达到最大线程数。此时会将任务加入工作队列中工作队列采用的是无界的阻塞队列,支持先提交的先执行。因为LinkedBlockingQueue队列是无参构造,默认最大可存放请求数为 Integer.MAX_VALUE ,当任务量过多时,可能会导致大量任务堆积到队列中,从而导致 OOM。
newSingleThreadExecutor()
/**
* 创建一个Executor,该Executor使用单个工作线程对无界队列进行操作。
* (但是请注意,如果这个线程在关闭之前的执行期间由于失败而终止,如果需要执行后续任务,
* 将会有一个新的线程取代它的位置。)
* 任务保证按顺序执行,并且在任何给定时间活动的任务不超过一个。与newFixedThreadPool(1)不同,
* 返回的执行器保证不能重新配置以使用其他线程。
* @return the newly created single-threaded Executor
*/
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()));
}
从源码中可以看到核心线程数是1,最大线程数也是1,队列采用的是无界的LinkedBlockingQueue阻塞队列。如果核心线程异常的话,则创建一个线程去顶替核心线程(但始终保持单线程),因为队列是无参构造,默认最大可存放请求数为 Integer.MAX_VALUE ,当任务量过多时,可能会导致大量任务堆积到队列中,从而导致 OOM。
newScheduledThreadPool(int corePoolSize)
/**
* 创建一个线程池,该线程池可以安排命令在给定延迟后运行,或定期执行。
* @param corePoolSize the number of threads to keep in the pool,
* even if they are idle
* @return a newly created scheduled thread pool
* @throws IllegalArgumentException if {@code corePoolSize < 0}
*/
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
return new ScheduledThreadPoolExecutor(corePoolSize);
}
//ScheduledThreadPoolExecutor源码
public ScheduledThreadPoolExecutor(int corePoolSize) {
super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
new DelayedWorkQueue());
}
该线程池是支持定时任务的线程池,可以指定核心线程数,最大线程数为Interger.MAX_VALUE,内部使用的是:DelayedWorkQueue无界优先级阻塞队列。要求元素都实现 Delayed 接口。因为该线程池的最大线程数量为 Integer.MAX_VALUE,任务量过多时,可能会创建大量的线程,从而导致 OOM。
总结
newFixedThreadPool和newSingleThreadExecutor: 主要问题是堆积的请求处理队列可能会耗费非常大的内存,甚至OOM。
newCachedThreadPool和newScheduledThreadPool: 问题是最大线程数为Integer.MAX_VALUE,可能会创建数量非常多的线程,甚至OOM。
还没有评论,来说两句吧...