当前位置:网站首页>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();
}
}
}
边栏推荐
- 来了!GaussDB(for Cassandra)新特性亮相
- Nat address translation
- Tips for short-term operation of spot silver that cannot be ignored
- Do you really understand sticky bag and half bag? 3 minutes to understand it
- Introduction de l'API commune de programmation de socket et mise en œuvre de socket, select, Poll et epoll
- [trusted computing] Lesson 11: TPM password resource management (III) NV index and PCR
- CVPR 2022丨学习用于小样本语义分割的非目标知识
- Wireshark分析抓包数据*.cap
- Improve application security through nonce field of play integrity API
- Personal best practice demo sharing of enum + validation
猜你喜欢

AI 击败了人类,设计了更好的经济机制
![[Tawang methodology] Tawang 3W consumption strategy - U & a research method](/img/63/a8c08ac6ec7d654159e5fc8b4423e4.png)
[Tawang methodology] Tawang 3W consumption strategy - U & a research method

Five network IO models

Charles+drony的APP抓包

手撕Nacos源码(先撕客户端源码)

CVPR 2022 - learning non target knowledge for semantic segmentation of small samples

Using stored procedures, timers, triggers to solve data analysis problems

海量数据去重的hash,bitmap与布隆过滤器Bloom Filter

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

Improve application security through nonce field of play integrity API
随机推荐
元宇宙带来的创意性改变
[sword finger offer] 59 - I. maximum value of sliding window
嵌入式C语言程序调试和宏使用的技巧
Ten thousand words nanny level long article -- offline installation guide for datahub of LinkedIn metadata management platform
DataSimba推出微信小程序,DataNuza接受全场景考验? | StartDT Hackathon
nest. Database for getting started with JS
Improve application security through nonce field of play integrity API
伺服力矩控制模式下的力矩目标值(fTorque)计算
C语言中匿名的最高境界
Save the memory of the model! Meta & UC Berkeley proposed memvit. The modeling time support is 30 times longer than the existing model, and the calculation amount is only increased by 4.5%
go语言的字符串类型、常量类型和容器类型
Comparison and selection of kubernetes Devops CD Tools
小试牛刀之NunJucks模板引擎
[principle and technology of network attack and Defense] Chapter 6: Trojan horse
回归问题的评价指标和重要知识点总结
上市十天就下线过万台,欧尚Z6产品实力备受点赞
Learn to make dynamic line chart in 3 minutes!
磁盘存储链式的B树与B+树
Charles+Postern的APP抓包
Kubernetes DevOps CD工具对比选型