IDEA的阿里巴巴规约插件提示:
“ 线程池不允许使用Executors去创建,而是通过ThreadPoolExecutor的方式,这样的处理方式让写的同学更加明确线程池的运行规则,规避资源耗尽的风险。”
即:不能使用
ExecutorService excutorService = Executors.newCachedThreadPool();
改为ThreadPoolExecutor:
ThreadPoolExecutor pool1 = new ThreadPoolExecutor(5,200,60,
TimeUnit.SECONDS,new ArrayBlockingQueue<Runnable> (5),Executors.defaultThreadFactory());
或
ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
.setNameFormat("demo-pool-%d").build();
ExecutorService pool = new ThreadPoolExecutor(5, 200,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(1024), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());
注意需要引入guava包,否则ThreadFactoryBuilder会报错 :
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>28.1-jre</version>
</dependency>
更多参考:
https://blog.csdn.net/qq_29428215/article/details/85784071
https://blog.csdn.net/weixin_41888813/article/details/90769126