当前位置:网站首页>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);
}
}
边栏推荐
- matlab保存DB4i深度相机图片
- 2022茶艺师(初级)操作证考试题库及模拟考试
- [website architecture] solve 90% of distributed transactions in one move, and introduce the working principles and application scenarios of database transactions and distributed transactions
- LSTM of RNN
- 2022年流动式起重机司机考试练习题及在线模拟考试
- SharePoint - how to quickly check whether SharePoint is standard or enterprise edition?
- Implementation and encapsulation of go universal dynamic retry mechanism
- Todolist classic case ①
- 2022电工(中级)复训题库及答案
- [MySQL learning notes 28] storage function
猜你喜欢

关系数据库如何工作

web254
![[programming compulsory training 3] find the longest consecutive number string in the string + the number that appears more than half of the times in the array](/img/5e/068268857b8c7c2065ba552fd23dbb.png)
[programming compulsory training 3] find the longest consecutive number string in the string + the number that appears more than half of the times in the array

【网站架构】一招搞定90%的分布式事务,实打实介绍数据库事务、分布式事务的工作原理应用场景

2022 tea master (intermediate) recurrent training question bank and answers

Conscience Amway universal wheel SolidWorks model material website
![Those high-frequency written tests and interview questions in [Jianzhi offer & Niuke 101] - linked list](/img/9a/44976b5df5567a7aff315e63569f6a.png)
Those high-frequency written tests and interview questions in [Jianzhi offer & Niuke 101] - linked list

Latex table

【mysql学习笔记25】sql语句优化

2022 Guangdong Provincial Safety Officer a certificate third batch (main person in charge) special operation certificate examination question bank simulated examination platform operation
随机推荐
Two expressions of string
Li Kou daily question - Day 32 -1822 Symbol of array element product
【mysql学习笔记25】sql语句优化
LM08丨网格系列之网格反转(精)
Long way to go with technology
Software testing methods and techniques - overview of basic knowledge
【R语言】年龄性别频数匹配 挑选样本 病例对照研究,对年龄性别进行频数匹配
【mysql学习笔记28】存储函数
[microservice openfeign] feign's log record
如何让两融交易更极速
ContentType所有类型对比
【mysql学习笔记26】视图
Is it safe and reliable for Huatai Securities to open an account? How to open Huatai Securities Account
关系数据库如何工作
Autosar 学习记录(1) – EcuM_Init
How to get a SharePoint online site created using the office365 group template
Do securities account opening affect the security of account opening
长路漫漫、技术作伴
下载Xshell和Xftp
【批处理DOS-CMD-汇总】扩展变量-延迟变量cmd /v:on、cmd /v:off、setlocal enabledelayedexpansion、DisableDelayedExpansion