当前位置:网站首页>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);
}
}
边栏推荐
- Scala语言学习-07-构造器
- 凸印的印刷原理及工艺介绍
- Li Kou daily question - day 31 -1502 Judge whether an arithmetic sequence can be formed
- How to use layui to display the data in the database in the form of tables
- Latex table
- Li Kou daily question - day 31 -202 Happy number
- [软件] phantomjs屏幕截图
- Android screen adaptation (using constraintlayout), kotlin array sorting
- 038 network security JS
- Li Kou daily question - Day 32 -1822 Symbol of array element product
猜你喜欢
随机推荐
Vhost kick & call principle
redisson使用全解——redisson官方文档+注释(中篇)
2022 electrician (intermediate) recurrent training question bank and answers
Download xshell and xftp
How to get a SharePoint online site created using the office365 group template
PWN attack and defense world int_ overflow
weback5基础配置详解
论文学习——水文时间序列相似性查询的分析与研究
组件的自定义事件①
Source code analysis of open source API gateway APIs IX
源代码加密的意义和措施
[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
[skill] create Bat quick open web page
web254
LM08丨网格系列之网格反转(精)
How to use layui to display the data in the database in the form of tables
Saving db4i depth camera pictures with MATLAB
Browser local storage
[software] phantomjs screenshot
Thesis learning -- Analysis and Research on similarity query of hydrological time series









