当前位置:网站首页>线程池的拒绝策略
线程池的拒绝策略
2022-07-07 16:37:00 【珠峰下的沙砾】
使用线程池解决了我们手动去创建线程的过程,同时也可以实现线程的复用;大大节省更多资源和时间。
我们可以定制不同的线程池,方便为以后提交任务使用。往往有时候我们设置的线程数量满足不了使用场景时,线程池就会通过一些策略处理超出的任务。下面我们看看线程池是如何解决任务超出。
线程池的拒绝策略父接口
// 拒绝执行处理程序
public interface RejectedExecutionHandler {
// 当提交的任务超出了线程池的处理范围时,,可能由ThreadPoolExecutor调用的方法。
void rejectedExecution(Runnable r, ThreadPoolExecutor executor);
}
AbortPolicy(默认策略)
// 抛出异常RejectedExecutionException
public static class AbortPolicy implements RejectedExecutionHandler {
/** * Creates an {@code AbortPolicy}. */
public AbortPolicy() {
}
// 抛出异常
// xx的任务被xx线程池拒绝
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
throw new RejectedExecutionException("Task " + r.toString() +
" rejected from " +
e.toString());
}
}
DiscardPolicy
// 它默默地丢弃被拒绝的任务。
public static class DiscardPolicy implements RejectedExecutionHandler {
/** * Creates a {@code DiscardPolicy}. */
public DiscardPolicy() {
}
// 什么都不做,具有丢弃任务 r 的效果
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
}
}
DiscardOldestPolicy
// 丢弃最旧的未处理请求,然后重试execute ,除非执行程序被关闭,在这种情况下任务被丢弃。
public static class DiscardOldestPolicy implements RejectedExecutionHandler {
/** * Creates a {@code DiscardOldestPolicy} for the given executor. */
public DiscardOldestPolicy() {
}
// 获取并忽略 executor 将执行的下一个任务,如果一个任务立即可用,然后重试任务 r 的 执行,除非 executor 被关闭,在这种情况下任务 r 被丢弃
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
// 检查线程池是否关闭,
// 没有关闭则进入方法
// 关闭则将任务丢弃
if (!e.isShutdown()) {
// 将阻塞队列在最后一个任务移除
e.getQueue().poll();
// 将新的任务提交放入队列中
e.execute(r);
}
}
}
CallerRunsPolicy
// 直接在execute方法的调用线程中运行被拒绝的任务,除非执行程序已关闭,在这种情况下,任务将被丢弃。
public static class CallerRunsPolicy implements RejectedExecutionHandler {
/** * Creates a {@code CallerRunsPolicy}. */
public CallerRunsPolicy() {
}
// 在调用者的线程中执行任务 r,除非执行者已经关闭,在这种情况下任务被丢弃。
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
// 检查线程池是否关闭
// 没有关闭则进入方法
// 反之丢弃任务
if (!e.isShutdown()) {
// 调用run方法执行任务
// 在调用者线程中执行此任务
r.run();
}
}
}
边栏推荐
- Chapter 1 Introduction to CRM core business
- Yearning-SQL审核平台
- Tips for this week 140: constants: safety idioms
- 2021年全国平均工资出炉,你达标了吗?
- 2022年理财产品的一般收益率是多少?
- Download, installation and development environment construction of "harmonyos" deveco
- [principles and technologies of network attack and Defense] Chapter 5: denial of service attack
- gsap动画库
- Taffydb open source JS database
- Disk storage chain B-tree and b+ tree
猜你喜欢
< code random recording two brushes> linked list
idea彻底卸载安装及配置笔记
Datasimba launched wechat applet, and datanuza accepted the test of the whole scene| StartDT Hackathon
[trusted computing] Lesson 12: TPM authorization and conversation
Performance test process and plan
Introduction of common API for socket programming and code implementation of socket, select, poll, epoll high concurrency server model
静态路由配置
Taffydb open source JS database
不能忽略的现货白银短线操作小技巧
你真的理解粘包与半包吗?3分钟搞懂它
随机推荐
Slider plug-in for swiper left and right switching
万字保姆级长文——Linkedin元数据管理平台Datahub离线安装指南
小程序中实现付款功能
五种网络IO模型
Do you really understand sticky bag and half bag? 3 minutes to understand it
String type, constant type and container type of go language
CVPR 2022丨学习用于小样本语义分割的非目标知识
SD_DATA_RECEIVE_SHIFT_REGISTER
数学分析_笔记_第11章:Fourier级数
国内的软件测试会受到偏见吗
小试牛刀之NunJucks模板引擎
[paper sharing] where's crypto?
嵌入式C语言程序调试和宏使用的技巧
直播软件搭建,canvas文字加粗
【C语言】字符串函数
『HarmonyOS』DevEco的下载安装与开发环境搭建
Some key points in the analysis of spot Silver
How to open an account for wealth securities? Is it safe to open a stock account through the link
Tips for this week 140: constants: safety idioms
Tear the Nacos source code by hand (tear the client source code first)