当前位置:网站首页>线程池的拒绝策略
线程池的拒绝策略
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();
}
}
}
边栏推荐
- [tpm2.0 principle and Application guide] Chapter 5, 7 and 8
- 标准ACL与扩展ACL
- SQLite SQL exception near "with": syntax error
- Thread pool and singleton mode and file operation
- 备份阿里云实例-oss-browser
- 科学家首次观察到“电子漩涡” 有助于设计出更高效的电子产品
- pip相关命令
- 手撕Nacos源码(先撕客户端源码)
- 万字保姆级长文——Linkedin元数据管理平台Datahub离线安装指南
- Tips for this week 134: make_ Unique and private constructors
猜你喜欢
[principle and technology of network attack and Defense] Chapter 1: Introduction
Chapter 3 business function development (safe exit)
手撕Nacos源码(先撕客户端源码)
Wireshark分析抓包数据*.cap
性能测试过程和计划
Learn to make dynamic line chart in 3 minutes!
小试牛刀之NunJucks模板引擎
Five network IO models
[4500 word summary] a complete set of skills that a software testing engineer needs to master
Do you really understand sticky bag and half bag? 3 minutes to understand it
随机推荐
Test for 3 months, successful entry "byte", my interview experience summary
Mui side navigation anchor positioning JS special effect
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
What are the financial products in 2022? What are suitable for beginners?
Hash, bitmap and bloom filter for mass data De duplication
Year SQL audit platform
Is it safe to open an online futures account now? How many regular futures companies are there in China?
debian10系统问题总结
NAT地址转换
Discuss | what preparations should be made before ar application is launched?
pip相关命令
回归测试的分类
Usage of PHP interview questions foreach ($arr as $value) and foreach ($arr as $value)
简单几步教你如何看k线图图解
Five network IO models
go语言的字符串类型、常量类型和容器类型
Mobile pixel bird game JS play code
科学家首次观察到“电子漩涡” 有助于设计出更高效的电子产品
nest.js入门之 database
【蓝桥杯集训100题】scratch从小到大排序 蓝桥杯scratch比赛专项预测编程题 集训模拟练习题第17题