线程池阻塞策略完善

This commit is contained in:
Looly 2022-06-20 12:12:07 +08:00
parent 9c43ee5f7d
commit 461e72d0e0
2 changed files with 15 additions and 7 deletions

View File

@ -3,7 +3,7 @@
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------
# 5.8.4.M1 (2022-06-18) # 5.8.4.M1 (2022-06-20)
### 🐣新特性 ### 🐣新特性
* 【extra 】 Sftp增加构造重载支持超时pr#653@Gitee * 【extra 】 Sftp增加构造重载支持超时pr#653@Gitee
@ -11,6 +11,7 @@
* 【json 】 修改byte[]统一转换为数组形式issue#2377@Github * 【json 】 修改byte[]统一转换为数组形式issue#2377@Github
* 【http 】 HttpResponse增加body方法支持自定义返回内容pr#655@Gitee * 【http 】 HttpResponse增加body方法支持自定义返回内容pr#655@Gitee
* 【core 】 修改ObjectUtil.isNull逻辑issue#I5COJF@Gitee * 【core 】 修改ObjectUtil.isNull逻辑issue#I5COJF@Gitee
* 【core 】 BlockPolicy增加线程池关闭后的逻辑pr#660@Gitee
* *
### 🐞Bug修复 ### 🐞Bug修复
* 【extra 】 修复createExtractor中抛出异常后流未关闭问题pr#2384@Github * 【extra 】 修复createExtractor中抛出异常后流未关闭问题pr#2384@Github

View File

@ -21,10 +21,18 @@ public class BlockPolicy implements RejectedExecutionHandler {
*/ */
private final Consumer<Runnable> handlerwhenshutdown; private final Consumer<Runnable> handlerwhenshutdown;
/**
* 构造
*
* @param handlerwhenshutdown 线程池关闭后的执行策略
*/
public BlockPolicy(final Consumer<Runnable> handlerwhenshutdown) { public BlockPolicy(final Consumer<Runnable> handlerwhenshutdown) {
this.handlerwhenshutdown = handlerwhenshutdown; this.handlerwhenshutdown = handlerwhenshutdown;
} }
/**
* 构造
*/
public BlockPolicy() { public BlockPolicy() {
this(null); this(null);
} }
@ -32,18 +40,17 @@ public class BlockPolicy implements RejectedExecutionHandler {
@Override @Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) { public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
// 线程池未关闭时阻塞等待 // 线程池未关闭时阻塞等待
if(!e.isShutdown()){ if (false == e.isShutdown()) {
try { try {
e.getQueue().put(r); e.getQueue().put(r);
} catch (InterruptedException ex) { } catch (InterruptedException ex) {
throw new RejectedExecutionException("Task " + r + " rejected from " + e); throw new RejectedExecutionException("Task " + r + " rejected from " + e);
} }
return; } else if (null != handlerwhenshutdown) {
} // 当设置了关闭时候的处理
// 当设置了关闭时候的处理
if(null != handlerwhenshutdown){
handlerwhenshutdown.accept(r); handlerwhenshutdown.accept(r);
} }
// 线程池关闭后丢弃任务
} }
} }