From 461e72d0e0757ef438d3b490063c52ceeba3bba1 Mon Sep 17 00:00:00 2001 From: Looly Date: Mon, 20 Jun 2022 12:12:07 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BA=BF=E7=A8=8B=E6=B1=A0=E9=98=BB=E5=A1=9E?= =?UTF-8?q?=E7=AD=96=E7=95=A5=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 3 ++- .../cn/hutool/core/thread/BlockPolicy.java | 19 +++++++++++++------ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2eabe1062..bdf93874f 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ ------------------------------------------------------------------------------------------------------------- -# 5.8.4.M1 (2022-06-18) +# 5.8.4.M1 (2022-06-20) ### 🐣新特性 * 【extra 】 Sftp增加构造重载,支持超时(pr#653@Gitee) @@ -11,6 +11,7 @@ * 【json 】 修改byte[]统一转换为数组形式(issue#2377@Github) * 【http 】 HttpResponse增加body方法,支持自定义返回内容(pr#655@Gitee) * 【core 】 修改ObjectUtil.isNull逻辑(issue#I5COJF@Gitee) +* 【core 】 BlockPolicy增加线程池关闭后的逻辑(pr#660@Gitee) * ### 🐞Bug修复 * 【extra 】 修复createExtractor中抛出异常后流未关闭问题(pr#2384@Github) diff --git a/hutool-core/src/main/java/cn/hutool/core/thread/BlockPolicy.java b/hutool-core/src/main/java/cn/hutool/core/thread/BlockPolicy.java index 745c00dc5..eb3b82f6f 100755 --- a/hutool-core/src/main/java/cn/hutool/core/thread/BlockPolicy.java +++ b/hutool-core/src/main/java/cn/hutool/core/thread/BlockPolicy.java @@ -21,10 +21,18 @@ public class BlockPolicy implements RejectedExecutionHandler { */ private final Consumer handlerwhenshutdown; + /** + * 构造 + * + * @param handlerwhenshutdown 线程池关闭后的执行策略 + */ public BlockPolicy(final Consumer handlerwhenshutdown) { this.handlerwhenshutdown = handlerwhenshutdown; } + /** + * 构造 + */ public BlockPolicy() { this(null); } @@ -32,18 +40,17 @@ public class BlockPolicy implements RejectedExecutionHandler { @Override public void rejectedExecution(Runnable r, ThreadPoolExecutor e) { // 线程池未关闭时,阻塞等待 - if(!e.isShutdown()){ + if (false == e.isShutdown()) { try { e.getQueue().put(r); } catch (InterruptedException ex) { throw new RejectedExecutionException("Task " + r + " rejected from " + e); } - return; - } - - // 当设置了关闭时候的处理 - if(null != handlerwhenshutdown){ + } else if (null != handlerwhenshutdown) { + // 当设置了关闭时候的处理 handlerwhenshutdown.accept(r); } + + // 线程池关闭后,丢弃任务 } }