当前位置:网站首页>Insufficient executors to build thread pool
Insufficient executors to build thread pool
2022-07-01 07:55:00 【To scholars】
One summary
When building a thread pool, we can simply use Executors To build , With newFixedThreadPool For example :
public static ExecutorService executor = Executors.newFixedThreadPool(10);Two Executors Shortcomings of building threads
Thread pools are not allowed Executors To create , But through ThreadPoolExecutor The way , This processing method makes the developer more clear about the running rules of the thread pool , Avoid the risk of resource depletion . explain :Executors The disadvantages of the returned thread pool object are as follows :
1)FixedThreadPool and SingleThreadPool:
The allowed request queue length is Integer.MAX_VALUE, A large number of requests may pile up , Which leads to OOM.
2)CachedThreadPool:
The number of threads allowed to be created is Integer.MAX_VALUE, A large number of threads may be created , Which leads to OOM.
Suggested Java How it was created
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 shutdownSuggested XML How it was created
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);3、 ... and People often build thread pools
public class ThreadPool {
/**
* Thread pool executor
*/
private static ThreadPoolTaskExecutor EXECUTOR = new ThreadPoolTaskExecutor();
static {
// Number of core threads
EXECUTOR.setCorePoolSize(10);
// Maximum number of threads
EXECUTOR.setMaxPoolSize(20);
// Queue size
EXECUTOR.setQueueCapacity(200);
// Maximum number of threads - Number of core threads Idle survival time
EXECUTOR.setKeepAliveSeconds(60);
// The thread name prefix in the thread
EXECUTOR.setThreadNamePrefix("common-pool-");
// Rejection policy for thread pool
EXECUTOR.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
// Initialization of thread pool
EXECUTOR.initialize();
}
/**
* Get thread pool
* @param
*/
public static ThreadPoolTaskExecutor getExecutor() {
return EXECUTOR;
}
/**
* perform
* @param command
*/
public static void execute(Runnable command) {
EXECUTOR.execute(command);
}
}
边栏推荐
- The database is locked. Is there a solution
- 2022 operation of refrigeration and air conditioning equipment operation of national question bank simulated examination platform
- 2022茶艺师(初级)操作证考试题库及模拟考试
- 论文学习——水文时间序列相似性查询的分析与研究
- Long way to go with technology
- Introduction to kubernetes resource objects and common commands (II)
- 2022广东省安全员A证第三批(主要负责人)特种作业证考试题库模拟考试平台操作
- 【批处理DOS-CMD-汇总】扩展变量-延迟变量cmd /v:on、cmd /v:off、setlocal enabledelayedexpansion、DisableDelayedExpansion
- 【技能】创建.bat快速打开网页
- 【微服务|openfeign】Feign的日志记录
猜你喜欢

Why some people earn nearly 10billion a year, while others earn 3000 a month: the details you ignore actually make the most money

2022危险化学品经营单位主要负责人试题及模拟考试

凸印的印刷原理及工艺介绍

Download xshell and xftp

Eigen matrix operation Library

Wang Yingqi, founder of ones, talks to fortune (Chinese version): is there any excellent software in China?

base64

Caesar
![[recommendation system] breakthrough and imagination of deep location interactive network dpin for meituan takeout recommendation scenario](/img/10/ed857892d2e0ea72e100a4008e6d69.png)
[recommendation system] breakthrough and imagination of deep location interactive network dpin for meituan takeout recommendation scenario

如何使用layui将数据库中的数据以表格的形式展现出来
随机推荐
下载Xshell和Xftp
[microservice openfeign] feign's log record
[batch DOS CMD summary] extension variables - delay variables CMD /v:on, CMD /v:off, SETLOCAL enabledelayedexpansion, disabledelayedexpansion
【mysql学习笔记27】存储过程
kubernetes资源对象介绍及常用命令(二)
MATLAB之基础知识
Is it safe and reliable for Huatai Securities to open an account? How to open Huatai Securities Account
Long way to go with technology
长路漫漫、技术作伴
[untitled]
Custom events of components ②
Rk3399 platform development series explanation (network debugging) 7.30. What will affect the sending process of TCP packets?
2022电工(中级)复训题库及答案
Redisson watchdog mechanism, redisson watchdog performance problems, redisson source code analysis
redisson使用全解——redisson官方文档+注释(下篇)
Implementation and encapsulation of go universal dynamic retry mechanism
H5 页面设置了字体的粗细样式,但是在华为手机里微信打开访问样式不生效?
Kickback -- find the first palindrome character in a group of characters
LSTM of RNN
How to check ad user information?