当前位置:网站首页>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 shutdown
Suggested 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);
}
}
边栏推荐
- [软件] phantomjs屏幕截图
- Browser local storage
- Missing API interface actual development series (14): ID card real name authentication verification
- [MySQL learning notes 28] storage function
- Long way to go with technology
- How to check ad user information?
- The triode is a great invention
- 2022 Guangdong Provincial Safety Officer a certificate third batch (main person in charge) special operation certificate examination question bank simulated examination platform operation
- Todolist classic case ①
- [MySQL learning notes 25] SQL statement optimization
猜你喜欢
The triode is a great invention
良心安利万向轮 SolidWorks模型素材网站
[programming training] delete public characters (hash mapping) + team competition (greedy)
Latex formula code
Redisson utilise la solution complète - redisson Documents officiels + commentaires (Partie 1)
【批处理DOS-CMD-汇总】扩展变量-延迟变量cmd /v:on、cmd /v:off、setlocal enabledelayedexpansion、DisableDelayedExpansion
Android screen adaptation (using constraintlayout), kotlin array sorting
Minecraft 1.16.5 module development (51) tile entity
2022 Guangdong Provincial Safety Officer a certificate third batch (main person in charge) special operation certificate examination question bank simulated examination platform operation
Teach you how to apply for domestic trademark online step by step
随机推荐
LSTM of RNN
vscode 根据 ESLint 规范自动格式化代码
良心安利万向轮 SolidWorks模型素材网站
Long way to go with technology
Stepsister becomes stepmother, son breaks off relationship with himself, and musk, the world's richest man, why is it so miserable?
[programming training 2] sorting subsequence + inverted string
力扣——求一组字符中的第一个回文字符
Principle and process of embossing
[R language] two /n data merge functions
【R语言】两个/N个数据合并merge函数
[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
TodoList经典案例①
力扣每日一题-第31天-1502.判断能否形成等差数列
Thesis learning -- Analysis and Research on similarity query of hydrological time series
Two expressions of string
Huawei modelarts training alexnet model
【技能】创建.bat快速打开网页
Latex formula code
Cadence OrCAD capture "network name" is the same, but it is not connected or connected incorrectly. The usage of nodeName of liberation scheme
源代码加密的意义和措施