当前位置:网站首页>Mature threads should know how to refuse
Mature threads should know how to refuse
2022-07-19 04:29:00 【chinesszz】

author : TaoYuanming of the Western Wei Dynasty
Blog : https://blog.springlearn.cn/
TaoYuanming of the Western Wei Dynasty
Don't laugh at the young Jianghu dream , Who doesn't dream of Jianghu
Refusal strategy
The rejection strategy is that the task can't be implemented , Then you need to tell the program , How to refuse to perform other tasks
In the actual development scenario , Basic use JDK With its own strategy, daily development can be completed , But as a programmer, you must know . Let's talk about it JDK What threading strategies are included
public interface RejectedExecutionHandler {
void rejectedExecution(Runnable r, ThreadPoolExecutor executor);
}
Four rejection strategies
ThreadPoolExecutor Class is built-in 4 Middle refusal strategy , Let's analyze... One by one
1. CallerRunsPolicy
Perform the task directly , If the thread pool is closed , It doesn 't run
public static class CallerRunsPolicy implements RejectedExecutionHandler {
public CallerRunsPolicy() {
}
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
if (!e.isShutdown()) {
r.run();
}
}
}
2. AbortPolicy 【 The default policy 】
Report an exception directly , Simple and crude
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
Direct discarding , No information is recorded
public static class DiscardPolicy implements RejectedExecutionHandler {
public DiscardPolicy() {
}
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
}
}
4. DiscardOldestPolicy
Discard an old task , And then perform the current task
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 Remove the first task , That is, the old task
e.getQueue().poll();
e.execute(r);
}
}
analysis
The rejection strategy is actually very simple , Knowing one's own and knowing one's other will win every battle , In the scenario of multithreading and multitasking programming , We can define rejection policies according to business characteristics .
such as , When the task is full, it will , Put the task into the database , Or print to a special log , Used to recover tasks .
Finally, please pay attention , Request subscription , Thanks for reading !

边栏推荐
- Machine learning 10: Integrated Learning
- 小程序毕设作品之微信电子书阅读小程序毕业设计(2)小程序功能
- Hello World driver
- 状态码含义详解
- Android kotlin自定义自动换行LinearLayout
- Wechat e-book reading applet graduation project (8) graduation project thesis template
- Unity - how to modify a package or localize it
- leetcode209. Minimum length subarray
- MAUI 框架入门学习05 MVVM数据模型理解
- Codeforces Round #807 (Div. 2) A~D
猜你喜欢

Heartless sword Chinese English bilingual poem 005 Lyric

HCR Huichen is walking on the north slope, a giant beast swimming into digital marketing services

DNS原理及解析过程

Touchid and faceid~1

06 Maui, WPF uses MVVM toolkit framework to build MVVM program

对称加密与非对称加密

Data types of basic knowledge of C language

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

OSPF的优化和配置

priority_ Introduction and use of queue
随机推荐
Brief introduction to cmtime
sql中的substr与substring函数用法
donet framework4. X==windows form application new project, through system Data. SqlClient connects to sqlserver to query
Wechat e-book reading applet graduation project (8) graduation project thesis template
OSPF basic optimization
[super cloud terminal to create a leading opportunity] local computing cloud management, Intel helps digitalize Education
The adaptation of go language under windows10:vscode
Typeorm MySQL upsert operation
MAUI 框架入門學習05 MVVM數據模型理解
In the era of super video, what is the solution to the data flood?
WPF cannot find resource file problem
Android kotlin custom LinearLayout
word使用技巧
AttributeError: ‘NoneType‘ object has no attribute ‘sort‘
机器学习10:集成学习
Set administrator permissions for idea and console
OSPF路由控制,防环相关知识
mysql中的视图、触发器和存储过程
By voting for the destruction of STI by Dao, seektiger is truly community driven
ffmpeg中AVFrame\AVPacket与自己的数据交互