当前位置:网站首页>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();
}
}
}
边栏推荐
- Yunjing network technology interview question [Hangzhou multi tester] [Hangzhou multi tester _ Wang Sir]
- Using stored procedures, timers, triggers to solve data analysis problems
- C语言中匿名的最高境界
- 持续测试(CT)实战经验分享
- [principles and technologies of network attack and Defense] Chapter 5: denial of service attack
- [论文分享] Where’s Crypto?
- How to clean when win11 C disk is full? Win11 method of cleaning C disk
- Cloud security daily 220707: Cisco Expressway series and telepresence video communication server have found remote attack vulnerabilities and need to be upgraded as soon as possible
- socket編程之常用api介紹與socket、select、poll、epoll高並發服務器模型代碼實現
- idea彻底卸载安装及配置笔记
猜你喜欢
用存储过程、定时器、触发器来解决数据分析问题
万字保姆级长文——Linkedin元数据管理平台Datahub离线安装指南
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
标准ACL与扩展ACL
备份阿里云实例-oss-browser
GSAP animation library
C语言中匿名的最高境界
Charles+drony的APP抓包
[unity shader] insert pass to realize the X-ray perspective effect of model occlusion
Yearning-SQL审核平台
随机推荐
回归问题的评价指标和重要知识点总结
[principles and technologies of network attack and Defense] Chapter 5: denial of service attack
Introduction of common API for socket programming and code implementation of socket, select, poll, epoll high concurrency server model
『HarmonyOS』DevEco的下载安装与开发环境搭建
Tips for this week 131: special member functions and ` = Default`
Live broadcast software construction, canvas Text Bold
将模型的记忆保存下来!Meta&UC Berkeley提出MeMViT,建模时间支持比现有模型长30倍,计算量仅增加4.5%...
回归测试的分类
国内的软件测试会受到偏见吗
[trusted computing] Lesson 10: TPM password resource management (II)
PHP面试题 foreach($arr as &$value)与foreach($arr as $value)的用法
String type, constant type and container type of go language
CVPR 2022丨学习用于小样本语义分割的非目标知识
How to clean when win11 C disk is full? Win11 method of cleaning C disk
简单几步教你如何看k线图图解
讨论 | AR 应用落地前,要做好哪些准备?
SQLite SQL exception near "with": syntax error
Kubernetes DevOps CD工具对比选型
Differences between rip and OSPF and configuration commands
【塔望方法论】塔望3W消费战略 - U&A研究法