脚本宝典收集整理的这篇文章主要介绍了阿里巴巴规范:手动创建线程池,效果会更好哦 的解决办法,手动创建线程池ThreadPoolExecutor,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
在使用Executors创建线程时,阿里巴巴规范提出了
手动创建线程池,效果会更好哦。
使用ThreadPoolExecutor
方式创建线程池,可以规避资源耗尽风险(OOM)
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler) {
if (corePoolSize < 0 ||
maximumPoolSize <= 0 ||
maximumPoolSize < corePoolSize ||
keepAliveTime < 0)
throw new IllegalArgumentException();
if (workQueue == null || threadFactory == null || handler == null)
throw new NullPointerException();
this.acc = System.getSecurityManager() == null ?
null :
AccessController.getContext();
this.corePoolSize = corePoolSize;
this.maximumPoolSize = maximumPoolSize;
this.workQueue = workQueue;
this.keepAliveTime = unit.toNanos(keepAliveTime);
this.threadFactory = threadFactory;
this.handler = handler;
}
线程池不允许使用Executors去创建,而是通过ThreadPoolExecutor的方式,这样的处理方式让写的同学更加明确线程池的运行规则,规避资源耗尽的风险。 说明:Executors返回的线程池对象的弊端如下:
1)FixedThreadPool和SingleThreadPool:
允许的请求队列长度为Integer.MAX_VALUE,可能会堆积大量的请求,从而导致OOM。
2)CachedThreadPool:
允许的创建线程数量为Integer.MAX_VALUE,可能会创建大量的线程,从而导致OOM。
Positive example 1:
//org.apache.commons.lang3.concurrent.BasicThreadFactory
ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(1,
new BasicThreadFactory.Builder().namingPattern("example-schedule-pool-%d").daemon(true).build());
Positive example 2:
ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
.setNameFormat("demo-pool-%d").build();
//Common Thread Pool
ExecutorService pool = new ThreadPoolExecutor(5, 200,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(1024), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());
pool.execute(()-> System.out.println(Thread.currentThread().getName()));
pool.shutdown();//gracefully shutdown
Positive example 3:
<bean id="userThreadPool"
class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name="corePoolSize" value="10" />
<property name="maxPoolSize" value="100" />
<property name="queueCapacity" value="2000" />
<property name="threadFactory" value= threadFactory />
<property name="rejectedExecutionHandler">
<ref local="rejectedExecutionHandler" />
</property>
</bean>
//in code
userThreadPool.execute(thread);
package com.demos.date.thread;
import cn.hutool.core.thread.ThreadFactoryBuilder;
import java.util.concurrent.*;
public class Thread01 {
public static void main(String[] args) {
// 编写线程的名字,一般使用默认即可,
ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
.setNamePrefix("demo-pool-%d").build();
ExecutorService pool = new ThreadPoolExecutor(2, 200,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(1024),namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());
// 调用线程
for (int i = 0; i < 4; i++) {
pool.execute(()->{
System.out.println(Thread.currentThread().getName()+Thread.currentThread().getId());
pool.shutdown();
});
}
}
}
以上是脚本宝典为你收集整理的阿里巴巴规范:手动创建线程池,效果会更好哦 的解决办法,手动创建线程池ThreadPoolExecutor全部内容,希望文章能够帮你解决阿里巴巴规范:手动创建线程池,效果会更好哦 的解决办法,手动创建线程池ThreadPoolExecutor所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。