当前位置:网站首页>线程池的拒绝策略
线程池的拒绝策略
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();
}
}
}
边栏推荐
- Tips of this week 135: test the contract instead of implementation
- The highest level of anonymity in C language
- [paddleseg source code reading] add boundary IOU calculation in paddleseg validation (1) -- val.py file details tips
- C语言中匿名的最高境界
- 『HarmonyOS』DevEco的下载安装与开发环境搭建
- 强化学习-学习笔记8 | Q-learning
- Interviewer: why is the page too laggy and how to solve it? [test interview question sharing]
- 行业案例|数字化经营底座助力寿险行业转型
- 直播软件搭建,canvas文字加粗
- [论文分享] Where’s Crypto?
猜你喜欢
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
【蓝桥杯集训100题】scratch从小到大排序 蓝桥杯scratch比赛专项预测编程题 集训模拟练习题第17题
数学分析_笔记_第11章:Fourier级数
socket編程之常用api介紹與socket、select、poll、epoll高並發服務器模型代碼實現
Year SQL audit platform
Slider plug-in for swiper left and right switching
[PaddleSeg源码阅读] PaddleSeg Validation 中添加 Boundary IoU的计算(1)——val.py文件细节提示
C语言中匿名的最高境界
回归问题的评价指标和重要知识点总结
[trusted computing] Lesson 12: TPM authorization and conversation
随机推荐
Datasimba launched wechat applet, and datanuza accepted the test of the whole scene| StartDT Hackathon
Tips for this week 131: special member functions and ` = Default`
【蓝桥杯集训100题】scratch从小到大排序 蓝桥杯scratch比赛专项预测编程题 集训模拟练习题第17题
Improve application security through nonce field of play integrity API
万字保姆级长文——Linkedin元数据管理平台Datahub离线安装指南
What is the general yield of financial products in 2022?
NAT地址转换
Tear the Nacos source code by hand (tear the client source code first)
More than 10000 units were offline within ten days of listing, and the strength of Auchan Z6 products was highly praised
Chapter 3 business function development (user access project)
Ten thousand words nanny level long article -- offline installation guide for datahub of LinkedIn metadata management platform
手撕Nacos源码(先撕客户端源码)
Summary of debian10 system problems
[trusted computing] Lesson 12: TPM authorization and conversation
[network attack and defense principle and technology] Chapter 4: network scanning technology
Click on the top of today's headline app to navigate in the middle
静态路由配置
Introduction of common API for socket programming and code implementation of socket, select, poll, epoll high concurrency server model
Five network IO models
[paper sharing] where's crypto?