当前位置:网站首页>成熟的线程要懂得拒绝
成熟的线程要懂得拒绝
2022-07-17 03:58:00 【chinesszz】

作者: 西魏陶渊明
博客: https://blog.springlearn.cn/
西魏陶渊明
莫笑少年江湖梦,谁不少年梦江湖
拒绝策略
拒绝策略就是任务实在是已经执行不了,那么就需要你告诉程序,怎么样去拒绝在执行其他任务
在实际开发场景中,基本使用JDK自带的策略就可以完成日常开发,但是作为程序员必须要知道。下面说一下JDK自带有哪些线程策略
public interface RejectedExecutionHandler {
void rejectedExecution(Runnable r, ThreadPoolExecutor executor);
}
四种拒绝策略
ThreadPoolExecutor类里面是内置了4中拒绝策略,我们一个一个来分析
1. CallerRunsPolicy
直接执行该任务,如果线程池已经关闭,就不运行
public static class CallerRunsPolicy implements RejectedExecutionHandler {
public CallerRunsPolicy() {
}
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
if (!e.isShutdown()) {
r.run();
}
}
}
2. AbortPolicy 【默认策略】
直接报异常,简单粗暴
public static class AbortPolicy implements RejectedExecutionHandler {
public AbortPolicy() {
}
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
throw new RejectedExecutionException("Task " + r.toString() +
" rejected from " +
e.toString());
}
}
3. DiscardPolicy
直接丢弃,不记录任何信息
public static class DiscardPolicy implements RejectedExecutionHandler {
public DiscardPolicy() {
}
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
}
}
4. DiscardOldestPolicy
丢弃一个老任务,然后执行当前任务
public static class DiscardOldestPolicy implements RejectedExecutionHandler {
public DiscardOldestPolicy() {
}
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
if (!e.isShutdown()) {
// Retrieves and removes the head of this queue 移出最头任务,也就是老任务
e.getQueue().poll();
e.execute(r);
}
}
分析
拒绝策略其实很简单,知己知彼百战百胜,在多线程多任务编程场景下,我们可以根据业务特性定义拒绝策略。
比如,在任务满的情况将,任务放到数据库中,或者打印到特殊的日志中,用来恢复任务。
最后求关注,求订阅,谢谢你的阅读!

边栏推荐
- leetcode209. 长度最小的子数组
- Wechat online education video on demand learning applet graduation project (4) opening report
- SQL interface switching cannot obtain focus
- Wechat online education video on demand learning applet graduation design (3) background function
- [super cloud terminal to create a leading opportunity] local computing cloud management, Intel helps digitalize Education
- [database] must know at the end of the term ----- Chapter 2 relational data model
- 小程序毕设作品之微信电子书阅读小程序毕业设计(5)任务书
- Technical writing guide for programmers to leave work early
- [seventh issue of notebook series] download and use of openvino pre training model
- 【数据库】期末必知必会-----第一章 数据库概述
猜你喜欢

ospf防环
![[seventh issue of notebook series] download and use of openvino pre training model](/img/74/d5958137ddabadef1724afda5656e1.png)
[seventh issue of notebook series] download and use of openvino pre training model

If by frame package name modifier

基于stm32f103的智能风扇系统

Openresty as a static resource server

C language explanation series - practice and consolidation of circular sentences, explanation of binary search

Wechat e-book reading applet graduation design of applet completion works (2) applet function

Simulation Implementation of library function
![[Huang ah code] Introduction to MySQL - 5. Database tips: a single column group by will, and multiple columns?](/img/4d/552c4251779705a6dfd4ceb59c4261.png)
[Huang ah code] Introduction to MySQL - 5. Database tips: a single column group by will, and multiple columns?

Touchid and faceid~2
随机推荐
分布式笔记(02)— 分布式缓存之 Redis(用途、特点、高可用方案 Redis Cluster、Twemproxy、Codis 简要说明)
MAUI 框架入門學習05 MVVM數據模型理解
【数据库】期末必知必会-----第六章 实验部分
MAUI 框架入门学习05 MVVM数据模型理解
Wechat e-book reading of small program graduation design (5) task book
机器学习10:集成学习
V4L2学习资料收集
Software testing - use cases
小程序毕设作品之微信在线教育视频点播学习小程序毕业设计(3)后台功能
JS模态框
Graphic verification code verification
Machine learning 11: cost sensitive learning
Data interaction between avframe\avpacket and itself in ffmpeg
若依框架包名修改器
C# 使用this关键字串联构造函数调用方法
64. 最小路径和:给定一个包含非负整数的 m x n 网格 grid ,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。 说明:每次只能向下或者向右移动一步。
Heartless sword Chinese English bilingual poem 005 Lyric
Chapter 0 performance platform godeye source code analysis - Course Introduction
Android kotlin自定义自动换行LinearLayout
小程序毕设作品之微信电子书阅读小程序毕业设计(8)毕业设计论文模板