为什么不推荐使用Executors线程池

电玩女神 2024-03-26 12:05 151阅读 0赞

Executors类提供了一些创建线程池的静态方法。但是阿里巴巴开发手册强制不允许使用Executors来创建线程池,下面根据源码分析为什么阿里巴巴开发手册不允许使用Executors类创建线程池。

9af1b5a55639768a136f8735ec937a78.png

  1. newCachedThreadPool

    /**

    1. * 创建一个线程池,根据需要创建新线程,但在以前构造的线程可用时重用它们。
    2. * 这些池通常会提高执行许多短期异步任务的程序的性能。对execute的调用将重用先前构造的线程(如果可用)。
    3. * 如果没有可用的线程,将创建一个新线程并将其添加到池中。60秒内未使用的线程将被终止并从缓存中删除。
    4. * 因此,空闲时间足够长的池不会消耗任何资源。
    5. * @return the newly created thread pool
    6. */
    7. public static ExecutorService newCachedThreadPool() {
    8. return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
    9. 60L, TimeUnit.SECONDS,
    10. new SynchronousQueue<Runnable>());
    11. }

从源码中可以看出,核心线程为0,最大线程数为Integer.MAX_VALUE,队列为SynchronousQueue,该队列要求只有线程获取任务的话才能加入队列中。如果线程池的大小超过处理任务的线程,那么就会回收空闲线程。因为该线程池的最大线程数量为 Integer.MAX_VALUE,任务量过多时,可能会创建大量的线程,从而导致 OOM。

  1. newFixedThreadPool(int nThreads)

    /**

    1. * 创建一个线程池,该线程池重用固定数量的线程,这些线程在共享无界队列上操作。
    2. * 在任何时候,最多nThreads线程将是活动的处理任务。
    3. * 如果在所有线程都处于活动状态时提交了额外的任务,它们将在队列中等待,直到有一个线程可用。
    4. * 任何线程在关闭之前的执行过程中由于失败而终止,如需要执行后续任务,将会有一个新的线程取代它的位置。
    5. * 在显式关闭池之前,池中的线程将一直存在。
    6. * @param nThreads the number of threads in the pool
    7. * @return the newly created thread pool
    8. * @throws IllegalArgumentException if {@code nThreads <= 0}
    9. */
    10. public static ExecutorService newFixedThreadPool(int nThreads) {
    11. return new ThreadPoolExecutor(nThreads, nThreads,
    12. 0L, TimeUnit.MILLISECONDS,
    13. new LinkedBlockingQueue<Runnable>());
    14. }

从源码中可以看出,该方法会创建一个固定大小的线程池。核心线程数和最大线程数一致,当接受一个任务后就创建一个线程直至达到最大线程数。此时会将任务加入工作队列中工作队列采用的是无界的阻塞队列,支持先提交的先执行。因为LinkedBlockingQueue队列是无参构造,默认最大可存放请求数为 Integer.MAX_VALUE ,当任务量过多时,可能会导致大量任务堆积到队列中,从而导致 OOM。

  1. newSingleThreadExecutor()

    /**

    1. * 创建一个Executor,该Executor使用单个工作线程对无界队列进行操作。
    2. * (但是请注意,如果这个线程在关闭之前的执行期间由于失败而终止,如果需要执行后续任务,
    3. * 将会有一个新的线程取代它的位置。)
    4. * 任务保证按顺序执行,并且在任何给定时间活动的任务不超过一个。与newFixedThreadPool(1)不同,
    5. * 返回的执行器保证不能重新配置以使用其他线程。
    6. * @return the newly created single-threaded Executor
    7. */
    8. public static ExecutorService newSingleThreadExecutor() {
    9. return new FinalizableDelegatedExecutorService
    10. (new ThreadPoolExecutor(1, 1,
    11. 0L, TimeUnit.MILLISECONDS,
    12. new LinkedBlockingQueue<Runnable>()));
    13. }

从源码中可以看到核心线程数是1,最大线程数也是1,队列采用的是无界的LinkedBlockingQueue阻塞队列。如果核心线程异常的话,则创建一个线程去顶替核心线程(但始终保持单线程),因为队列是无参构造,默认最大可存放请求数为 Integer.MAX_VALUE ,当任务量过多时,可能会导致大量任务堆积到队列中,从而导致 OOM。

  1. newScheduledThreadPool(int corePoolSize)

    /**

    1. * 创建一个线程池,该线程池可以安排命令在给定延迟后运行,或定期执行。
    2. * @param corePoolSize the number of threads to keep in the pool,
    3. * even if they are idle
    4. * @return a newly created scheduled thread pool
    5. * @throws IllegalArgumentException if {@code corePoolSize < 0}
    6. */
    7. public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
    8. return new ScheduledThreadPoolExecutor(corePoolSize);
    9. }
    10. //ScheduledThreadPoolExecutor源码
    11. public ScheduledThreadPoolExecutor(int corePoolSize) {
    12. super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
    13. new DelayedWorkQueue());
    14. }

该线程池是支持定时任务的线程池,可以指定核心线程数,最大线程数为Interger.MAX_VALUE,内部使用的是:DelayedWorkQueue无界优先级阻塞队列。要求元素都实现 Delayed 接口。因为该线程池的最大线程数量为 Integer.MAX_VALUE,任务量过多时,可能会创建大量的线程,从而导致 OOM。

总结

newFixedThreadPool和newSingleThreadExecutor: 主要问题是堆积的请求处理队列可能会耗费非常大的内存,甚至OOM。

newCachedThreadPool和newScheduledThreadPool: 问题是最大线程数为Integer.MAX_VALUE,可能会创建数量非常多的线程,甚至OOM。

发表评论

表情:
评论列表 (有 0 条评论,151人围观)

还没有评论,来说两句吧...

相关阅读