当前位置:网站首页>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();
}
}
}
边栏推荐
- Rules for filling in volunteers for college entrance examination
- Win11C盘满了怎么清理?Win11清理C盘的方法
- More than 10000 units were offline within ten days of listing, and the strength of Auchan Z6 products was highly praised
- 2022年理财有哪些产品?哪些适合新手?
- 小试牛刀之NunJucks模板引擎
- Tips for this week 140: constants: safety idioms
- 企业展厅设计中常用的三种多媒体技术形式
- 【蓝桥杯集训100题】scratch从小到大排序 蓝桥杯scratch比赛专项预测编程题 集训模拟练习题第17题
- 用存储过程、定时器、触发器来解决数据分析问题
- Year SQL audit platform
猜你喜欢
Improve application security through nonce field of play integrity API
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
Charles+drony的APP抓包
Chapter 3 business function development (safe exit)
AntiSamy:防 XSS 攻击的一种解决方案使用教程
AI 击败了人类,设计了更好的经济机制
小程序中实现付款功能
Using stored procedures, timers, triggers to solve data analysis problems
讨论| 坦白局,工业 AR 应用为什么难落地?
Five network IO models
随机推荐
Rules for filling in volunteers for college entrance examination
go语言的字符串类型、常量类型和容器类型
[Tawang methodology] Tawang 3W consumption strategy - U & a research method
A few simple steps to teach you how to see the K-line diagram
2022年推荐免费在线接收短信平台(国内、国外)
【demo】循环队列及条件锁实现goroutine间的通信
Live broadcast software construction, canvas Text Bold
[trusted computing] Lesson 12: TPM authorization and conversation
[C language] string function
debian10系统问题总结
Download, installation and development environment construction of "harmonyos" deveco
[tpm2.0 principle and Application guide] Chapter 5, 7 and 8
手撕Nacos源码(先撕客户端源码)
SQLite SQL exception near "with": syntax error
Ten thousand words nanny level long article -- offline installation guide for datahub of LinkedIn metadata management platform
低代码助力企业数字化转型会让程序员失业?
清华、剑桥、UIC联合推出首个中文事实核查数据集:基于证据、涵盖医疗社会等多个领域
Introduction of common API for socket programming and code implementation of socket, select, poll, epoll high concurrency server model
保证接口数据安全的10种方案
同消费互联网的较为短暂的产业链不同,产业互联网的产业链是相当漫长的