前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Executors Java编程规范插件提示手动创建线程池的解决办法

Executors Java编程规范插件提示手动创建线程池的解决办法

作者头像
明明如月学长
发布2021-08-27 16:21:17
4800
发布2021-08-27 16:21:17
举报
文章被收录于专栏:明明如月的技术专栏

一、背景

最近了解一下线程池,下载其中的代码并运行。

https://howtodoinjava.com/core-java/multi-threading/when-to-use-countdownlatch-java-concurrency-example-tutorial/

其中ApplicationStartupUtil这个类

代码语言:javascript
复制
package com.chujianyun;
import com.chujianyun.verifier.BaseHealthChecker;
import com.chujianyun.verifier.CacheHealthChecker;
import com.chujianyun.verifier.DatabaseHealthChecker;
import com.chujianyun.verifier.NetworkHealthChecker;
import java.util.concurrent.*;
public class ApplicationStartupUtil 
{
   private static BlockingQueue services;
   private static CountDownLatch latch;
   
   private ApplicationStartupUtil()
   {
   }
   
   private final static ApplicationStartupUtil INSTANCE = new ApplicationStartupUtil();
   
   public static ApplicationStartupUtil getInstance()
   {
      return INSTANCE;
   }
   
   public static boolean checkExternalServices() throws Exception
   {
      latch = new CountDownLatch(3);
      services = new ArrayBlockingQueue<>(3);
      services.add(new NetworkHealthChecker(latch));
      services.add(new CacheHealthChecker(latch));
      services.add(new DatabaseHealthChecker(latch));
      
      ExecutorService executorService = Executors.newFixedThreadPool(services.size());
      for(final Runnable v : services)
      {
            executorService.execute(v);
      }
      latch.await();

      for(final Runnable v : services)
      {
            BaseHealthChecker baseHealthChecker = (BaseHealthChecker) v;
         if( ! baseHealthChecker.isServiceUp())
         {
            return false;
         }
      }
      return true;
   }  
}

其中有下面代码:

代码语言:javascript
复制
ExecutorService executorService = Executors.newFixedThreadPool(services.size());

由于IDEA安装了阿里的Java编程规范检查插件,提示让手动创建线程池。

二、探究

查看newFixedThreadPool函数源码:

代码语言:javascript
复制
/**
 * Creates a thread pool that reuses a fixed number of threads
 * operating off a shared unbounded queue.  At any point, at most
 * {@code nThreads} threads will be active processing tasks.
 * If additional tasks are submitted when all threads are active,
 * they will wait in the queue until a thread is available.
 * If any thread terminates due to a failure during execution
 * prior to shutdown, a new one will take its place if needed to
 * execute subsequent tasks.  The threads in the pool will exist
 * until it is explicitly {@link ExecutorService#shutdown shutdown}.
 *
 * @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());
}

得知该函数最终调用的还是ThreadPoolExecutor构造方法。

因此上面一句可以改成:

代码语言:javascript
复制
int size = services.size();
ExecutorService executorService = new ThreadPoolExecutor(size,size,0L,TimeUnit.MILLISECONDS,new LinkedBlockingQueue());

但是又有提示,建议要为线程池中的线程设置名称:

仅此在构造方法后加入TreadFactory,大功告成 

代码语言:javascript
复制
ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("thread-call-runner-%d").build();
int size = services.size();
ExecutorService executorService = new ThreadPoolExecutor(size,size,0L,TimeUnit.MILLISECONDS,new LinkedBlockingQueue(),namedThreadFactory);

补充

本文用到的  .ThreadFactoryBuilder 来自  guava 工具包

com.google.common.util.concurrent.ThreadFactoryBuilder

代码语言:javascript
复制
    com.google.guava
    guava
    27.0-jre

三、为什么要这么做呢?

我们参考阿里巴巴的Java开发手册内容:

8.   【强制】线程池不允许使用Executors去创建,而是通过ThreadPoolExecutor的方式,这样的处理方式让写的同学更加明确线程池的运行规则,规避资源耗尽的风险。 说明:Executors各个方法的弊端: 1)    newFixedThreadPool和newSingleThreadExecutor:  主要问题是堆积的请求处理队列可能会耗费非常大的内存,甚至OOM。 2)    newCachedThreadPool和newScheduledThreadPool:  主要问题是线程数最大数是Integer.MAX_VALUE,可能会创建数量非常多的线程,甚至OOM。 9. 【强制】创建线程或线程池时请指定有意义的线程名称,方便出错时回溯。

我在此简单进一步解读一下:

1 newFixedThreadPool和newSingleThreadExecutor 由于最后一个参数即工作队列是:

代码语言:javascript
复制
    /**
     * Creates a thread pool that reuses a fixed number of threads
     * operating off a shared unbounded queue.  At any point, at most
     * {@code nThreads} threads will be active processing tasks.
     * If additional tasks are submitted when all threads are active,
     * they will wait in the queue until a thread is available.
     * If any thread terminates due to a failure during execution
     * prior to shutdown, a new one will take its place if needed to
     * execute subsequent tasks.  The threads in the pool will exist
     * until it is explicitly {@link ExecutorService#shutdown shutdown}.
     *
     * @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());
    }

链表类型的阻塞队列,而我们看其构造函数发现,默认队列大小是整数的最大值!!

代码语言:javascript
复制
    /**
     * Creates a {@code LinkedBlockingQueue} with a capacity of
     * {@link Integer#MAX_VALUE}.
     */
    public LinkedBlockingQueue() {
        this(Integer.MAX_VALUE);
    }

    /**
     * Creates a {@code LinkedBlockingQueue} with the given (fixed) capacity.
     *
     * @param capacity the capacity of this queue
     * @throws IllegalArgumentException if {@code capacity} is not greater
     *         than zero
     */
    public LinkedBlockingQueue(int capacity) {
        if (capacity <= 0) throw new IllegalArgumentException();
        this.capacity = capacity;
        last = head = new Node(null);
    }

所以如果请求太多,队列很可能就耗费内存非常大导致OOM.

但是他们的线程数是固定的,而且一般不会太大,所以不会因为创建过多线程而导致OOM。

2 newCachedThreadPool和newScheduledThreadPool:

代码语言:javascript
复制
    /**
     * Creates a thread pool that creates new threads as needed, but
     * will reuse previously constructed threads when they are
     * available.  These pools will typically improve the performance
     * of programs that execute many short-lived asynchronous tasks.
     * Calls to {@code execute} will reuse previously constructed
     * threads if available. If no existing thread is available, a new
     * thread will be created and added to the pool. Threads that have
     * not been used for sixty seconds are terminated and removed from
     * the cache. Thus, a pool that remains idle for long enough will
     * not consume any resources. Note that pools with similar
     * properties but different details (for example, timeout parameters)
     * may be created using {@link ThreadPoolExecutor} constructors.
     *
     * @return the newly created thread pool
     */
    public static ExecutorService newCachedThreadPool() {
        return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                      60L, TimeUnit.SECONDS,
                                      new SynchronousQueue());
    }

其中第最大线程池大小是整数的最大值,因此线程可能不断创建,乃至到整数的最大值个线程,很容易导致OOM.

其中工作队列使用的是 SynchronousQueue

源码头部的注释中有说明

代码语言:javascript
复制
 * A {@linkplain BlockingQueue blocking queue} in which each insert
 * operation must wait for a corresponding remove operation by another
 * thread, and vice versa.  A synchronous queue does not have any
 * internal capacity, not even a capacity of one.  You cannot
 * {@code peek} at a synchronous queue because an element is only
 * present when you try to remove it; you cannot insert an element
 * (using any method) unless another thread is trying to remove it;
 * you cannot iterate as there is nothing to iterate.  The
 * head of the queue is the element that the first queued
 * inserting thread is trying to add to the queue; if there is no such
 * queued thread then no element is available for removal and
 * {@code poll()} will return {@code null}.  For purposes of other
 * {@code Collection} methods (for example {@code contains}), a
 * {@code SynchronousQueue} acts as an empty collection.  This queue
 * does not permit {@code null} elements.

可以看出

代码语言:javascript
复制
A {@linkplain BlockingQueue blocking queue} in which each insert operation must wait for a corresponding remove operation by another thread, and vice versa.

该类型的阻塞队列每一个插入操作必须等待对应的元素被另一个线程所移除,反之亦然。

因此阻塞队列不会无限拓展而导致OOM。

因此我们理解一些原则的时候,学习的时候多注重源码分析非常有必要,其他细节有待以后深入研究。

另外我写的一篇线程池导致线上故障的一篇文章大家也可以参考一下

https://cloud.tencent.com/developer/article/1870312

如果觉得本文对你有帮助,欢迎点赞,欢迎关注我,如果有补充欢迎评论交流,我将努力创作更多更好的文章。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018/05/10 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、背景
  • 二、探究
  • 补充
    • 三、为什么要这么做呢?
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档