当前位置:网站首页>Reject policy of thread pool
Reject policy of thread pool
2022-07-07 18:48:00 【Gravel under Mount Everest】
Using thread pool solves the process of manually creating threads , At the same time, it can also realize the reuse of threads ; Greatly save more resources and time .
We can customize different thread pools , It is convenient for submitting tasks in the future . Sometimes the number of threads we set cannot meet the usage scenario , The thread pool will handle the exceeded tasks through some strategies . Next, let's take a look at how the thread pool solves the problem of task overrun .
Reject policy parent interface of thread pool
// Refuse to execute handler
public interface RejectedExecutionHandler {
// When the submitted task exceeds the processing range of the thread pool ,, Maybe by ThreadPoolExecutor Method called .
void rejectedExecution(Runnable r, ThreadPoolExecutor executor);
}
AbortPolicy( The default policy )
// Throw an exception RejectedExecutionException
public static class AbortPolicy implements RejectedExecutionHandler {
/** * Creates an {@code AbortPolicy}. */
public AbortPolicy() {
}
// Throw an exception
// xx The task of is xx Thread pool refused
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
throw new RejectedExecutionException("Task " + r.toString() +
" rejected from " +
e.toString());
}
}
DiscardPolicy
// It silently discards rejected tasks .
public static class DiscardPolicy implements RejectedExecutionHandler {
/** * Creates a {@code DiscardPolicy}. */
public DiscardPolicy() {
}
// Don't do anything? , With discard task r The effect of
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
}
}
DiscardOldestPolicy
// Discard the oldest unprocessed request , And then try again execute , Unless the execution program is closed , In this case, the task is discarded .
public static class DiscardOldestPolicy implements RejectedExecutionHandler {
/** * Creates a {@code DiscardOldestPolicy} for the given executor. */
public DiscardOldestPolicy() {
}
// Get and ignore executor The next task to be performed , If a task is immediately available , Then retry the task r Of perform , Unless executor Shut down , In this case, the task r To be discarded
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
// Check if the thread pool is closed ,
// If it is not closed, enter the method
// Close and discard the task
if (!e.isShutdown()) {
// Remove the blocking queue at the last task
e.getQueue().poll();
// Put the new task submission in the queue
e.execute(r);
}
}
}
CallerRunsPolicy
// Directly in execute Method to run a rejected task in the calling thread of the , Unless the execution program is closed , under these circumstances , The mission will be discarded .
public static class CallerRunsPolicy implements RejectedExecutionHandler {
/** * Creates a {@code CallerRunsPolicy}. */
public CallerRunsPolicy() {
}
// Execute the task in the caller's thread r, Unless the performer has closed , In this case, the task is discarded .
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
// Check if the thread pool is closed
// If it is not closed, enter the method
// On the contrary, discard the task
if (!e.isShutdown()) {
// call run Methods to perform tasks
// Execute this task in the caller thread
r.run();
}
}
}
边栏推荐
- Reinforcement learning - learning notes 8 | Q-learning
- Year SQL audit platform
- pip相关命令
- 备份阿里云实例-oss-browser
- Industry case | digital operation base helps the transformation of life insurance industry
- 来了!GaussDB(for Cassandra)新特性亮相
- The report of the state of world food security and nutrition was released: the number of hungry people in the world increased to 828million in 2021
- [Tawang methodology] Tawang 3W consumption strategy - U & a research method
- Performance test process and plan
- 小程序中实现付款功能
猜你喜欢
单臂路由和三层交换的简单配置
How to clean when win11 C disk is full? Win11 method of cleaning C disk
Performance test process and plan
Debian10 compile and install MySQL
RIP和OSPF的区别和配置命令
行业案例|数字化经营底座助力寿险行业转型
[Tawang methodology] Tawang 3W consumption strategy - U & a research method
Backup Alibaba cloud instance OSS browser
性能测试过程和计划
Industry case | digital operation base helps the transformation of life insurance industry
随机推荐
[C language] string function
debian10编译安装mysql
Tips for short-term operation of spot silver that cannot be ignored
现货白银分析中的一些要点
Idea completely uninstalls installation and configuration notes
Chapter 3 business function development (safe exit)
[principles and technologies of network attack and Defense] Chapter 3: network reconnaissance technology
Redis的发布与订阅
[principle and technology of network attack and Defense] Chapter 7: password attack technology Chapter 8: network monitoring technology
不能忽略的现货白银短线操作小技巧
Tips for this week 140: constants: safety idioms
保证接口数据安全的10种方案
Tear the Nacos source code by hand (tear the client source code first)
[sword finger offer] 59 - I. maximum value of sliding window
Backup Alibaba cloud instance OSS browser
gsap动画库
直播预约通道开启!解锁音视频应用快速上线的秘诀
[Tawang methodology] Tawang 3W consumption strategy - U & a research method
强化学习-学习笔记8 | Q-learning
The performance and efficiency of the model that can do three segmentation tasks at the same time is better than maskformer! Meta & UIUC proposes a general segmentation model with better performance t