当前位置:网站首页>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();
}
}
}
边栏推荐
- PIP related commands
- socket編程之常用api介紹與socket、select、poll、epoll高並發服務器模型代碼實現
- 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
- 你真的理解粘包与半包吗?3分钟搞懂它
- Skills of embedded C language program debugging and macro use
- 线程池的拒绝策略
- 海量数据去重的hash,bitmap与布隆过滤器Bloom Filter
- Tips of this week 141: pay attention to implicit conversion to bool
- 通过 Play Integrity API 的 nonce 字段提高应用安全性
- 企业展厅设计中常用的三种多媒体技术形式
猜你喜欢

Standard ACL and extended ACL

五种网络IO模型
![[trusted computing] Lesson 13: TPM extended authorization and key management](/img/96/3089e80441949d26e39ba43306edeb.png)
[trusted computing] Lesson 13: TPM extended authorization and key management

上市十天就下线过万台,欧尚Z6产品实力备受点赞

A few simple steps to teach you how to see the K-line diagram

Wireshark analyzes packet capture data * cap

Idea completely uninstalls installation and configuration notes
![[principles and technologies of network attack and Defense] Chapter 5: denial of service attack](/img/18/ac8b4c0dba4dd972df119d2f670416.png)
[principles and technologies of network attack and Defense] Chapter 5: denial of service attack

String type, constant type and container type of go language
![[paddleseg source code reading] add boundary IOU calculation in paddleseg validation (1) -- val.py file details tips](/img/f2/b6a0e5512b35cf1b695a8feecd0895.png)
[paddleseg source code reading] add boundary IOU calculation in paddleseg validation (1) -- val.py file details tips
随机推荐
Debian10 compile and install MySQL
海量数据去重的hash,bitmap与布隆过滤器Bloom Filter
Personal best practice demo sharing of enum + validation
学习open62541 --- [67] 添加自定义Enum并显示名字
How to open an account for wealth securities? Is it safe to open a stock account through the link
PIP related commands
Usage of PHP interview questions foreach ($arr as $value) and foreach ($arr as $value)
Tips of this week 141: pay attention to implicit conversion to bool
[unity shader] insert pass to realize the X-ray perspective effect of model occlusion
Wireshark分析抓包数据*.cap
[principle and technology of network attack and Defense] Chapter 1: Introduction
Disk storage chain B-tree and b+ tree
Year SQL audit platform
3分钟学会制作动态折线图!
云安全日报220707:思科Expressway系列和网真视频通信服务器发现远程攻击漏洞,需要尽快升级
Nunjuks template engine
标准ACL与扩展ACL
SQLite SQL exception near "with": syntax error
磁盘存储链式的B树与B+树
用存储过程、定时器、触发器来解决数据分析问题