当前位置:网站首页>线程池的拒绝策略
线程池的拒绝策略
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
- Interviewer: why is the page too laggy and how to solve it? [test interview question sharing]
- Chapter 3 business function development (safe exit)
- Do you really understand sticky bag and half bag? 3 minutes to understand it
- [unity shader] insert pass to realize the X-ray perspective effect of model occlusion
- 能同时做三个分割任务的模型,性能和效率优于MaskFormer!Meta&UIUC提出通用分割模型,性能优于任务特定模型!开源!...
- 不能忽略的现货白银短线操作小技巧
- 直播软件搭建,canvas文字加粗
- 讨论 | AR 应用落地前,要做好哪些准备?
- [论文分享] Where’s Crypto?
猜你喜欢

Ten thousand words nanny level long article -- offline installation guide for datahub of LinkedIn metadata management platform

Disk storage chain B-tree and b+ tree
![[C language] string function](/img/6c/c77e8ed5bf383b7c656f45b361940f.png)
[C language] string function

ICer知识点杂烩(后附大量题目,持续更新中)

CVPR 2022丨学习用于小样本语义分割的非目标知识
![[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

讨论 | AR 应用落地前,要做好哪些准备?

现货白银分析中的一些要点

Chapter 2 build CRM project development environment (database design)

Download, installation and development environment construction of "harmonyos" deveco
随机推荐
nest.js入门之 database
Learn to make dynamic line chart in 3 minutes!
Afghan interim government security forces launched military operations against a hideout of the extremist organization "Islamic state"
go语言的字符串类型、常量类型和容器类型
Unlike the relatively short-lived industrial chain of consumer Internet, the industrial chain of industrial Internet is quite long
【Unity Shader】插入Pass实现模型遮挡X光透视效果
Click on the top of today's headline app to navigate in the middle
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
将模型的记忆保存下来!Meta&UC Berkeley提出MeMViT,建模时间支持比现有模型长30倍,计算量仅增加4.5%...
[principle and technology of network attack and Defense] Chapter 6: Trojan horse
Sanxian Guidong JS game source code
保证接口数据安全的10种方案
能同时做三个分割任务的模型,性能和效率优于MaskFormer!Meta&UIUC提出通用分割模型,性能优于任务特定模型!开源!...
Chapter 2 build CRM project development environment (database design)
Chapter 1 Introduction to CRM core business
卖空、加印、保库存,东方甄选居然一个月在抖音卖了266万单书
[network attack and defense principle and technology] Chapter 4: network scanning technology
Usage of PHP interview questions foreach ($arr as $value) and foreach ($arr as $value)
Taffydb open source JS database
现在网上期货开户安全吗?国内有多少家正规的期货公司?