当前位置:网站首页>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();
}
}
}
边栏推荐
- Redis的发布与订阅
- 【Unity Shader】插入Pass实现模型遮挡X光透视效果
- Industry case | digital operation base helps the transformation of life insurance industry
- 【C语言】字符串函数
- idea彻底卸载安装及配置笔记
- Introduction de l'API commune de programmation de socket et mise en œuvre de socket, select, Poll et epoll
- DataSimba推出微信小程序,DataNuza接受全场景考验? | StartDT Hackathon
- Standard ACL and extended ACL
- 清华、剑桥、UIC联合推出首个中文事实核查数据集:基于证据、涵盖医疗社会等多个领域
- CVPR 2022 - learning non target knowledge for semantic segmentation of small samples
猜你喜欢

String type, constant type and container type of go language

不能忽略的现货白银短线操作小技巧

Ten thousand words nanny level long article -- offline installation guide for datahub of LinkedIn metadata management platform

Charles+Postern的APP抓包

强化学习-学习笔记8 | Q-learning

现货白银分析中的一些要点

4种常见的缓存模式,你都知道吗?

Download, installation and development environment construction of "harmonyos" deveco

gsap动画库
![[paper sharing] where's crypto?](/img/27/9b47bfcdff8307e63f2699d6a4e1b4.png)
[paper sharing] where's crypto?
随机推荐
能同时做三个分割任务的模型,性能和效率优于MaskFormer!Meta&UIUC提出通用分割模型,性能优于任务特定模型!开源!...
备份阿里云实例-oss-browser
Standard ACL and extended ACL
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
Five simple ways to troubleshoot with Stace
回归测试的分类
PHP面试题 foreach($arr as &$value)与foreach($arr as $value)的用法
[Tawang methodology] Tawang 3W consumption strategy - U & a research method
C语言中匿名的最高境界
sqlite sql 异常 near “with“: syntax error
通过 Play Integrity API 的 nonce 字段提高应用安全性
海量数据去重的hash,bitmap与布隆过滤器Bloom Filter
Simple configuration of single arm routing and layer 3 switching
Tips for this week 131: special member functions and ` = Default`
SQLite SQL exception near "with": syntax error
Yearning-SQL审核平台
Comparison and selection of kubernetes Devops CD Tools
磁盘存储链式的B树与B+树
[trusted computing] Lesson 10: TPM password resource management (II)
性能测试过程和计划