From d29b87c36fef72494ccc27a8c088dc3746333ab8 Mon Sep 17 00:00:00 2001 From: Looly Date: Sat, 26 Nov 2022 12:13:39 +0800 Subject: [PATCH] =?UTF-8?q?SyncFinisher=E7=BA=BF=E7=A8=8B=E5=90=8C?= =?UTF-8?q?=E6=AD=A5=E7=BB=93=E6=9D=9F=E5=99=A8=E6=B7=BB=E5=8A=A0=E7=AB=8B?= =?UTF-8?q?=E5=8D=B3=E7=BB=93=E6=9D=9F=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 2 ++ .../cn/hutool/core/thread/SyncFinisher.java | 26 ++++++++++--------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f490cc855..82302c129 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ ### 🐣新特性 * 【core 】 CharUtil.isBlankChar增加\u180e(pr#2738@Github) +* 【core 】 SyncFinisher线程同步结束器添加立即结束方法(pr#879@Gitee) +* ### 🐞Bug修复 * 【json 】 修复普通byte数组转JSONArray时的异常(pr#875@Gitee) * 【core 】 修复ArrayUtil.insert()不支持原始类型数组的问题(pr#874@Gitee) diff --git a/hutool-core/src/main/java/cn/hutool/core/thread/SyncFinisher.java b/hutool-core/src/main/java/cn/hutool/core/thread/SyncFinisher.java index ef3022abf..a990fae2f 100644 --- a/hutool-core/src/main/java/cn/hutool/core/thread/SyncFinisher.java +++ b/hutool-core/src/main/java/cn/hutool/core/thread/SyncFinisher.java @@ -23,7 +23,6 @@ import java.util.concurrent.ExecutorService; * sf.start() * * - * * @author Looly * @since 4.1.15 */ @@ -34,9 +33,13 @@ public class SyncFinisher implements Closeable { private ExecutorService executorService; private boolean isBeginAtSameTime; - /** 启动同步器,用于保证所有worker线程同时开始 */ + /** + * 启动同步器,用于保证所有worker线程同时开始 + */ private final CountDownLatch beginLatch; - /** 结束同步器,用于等待所有worker线程同时结束 */ + /** + * 结束同步器,用于等待所有worker线程同时结束 + */ private CountDownLatch endLatch; /** @@ -123,7 +126,7 @@ public class SyncFinisher implements Closeable { public void start(boolean sync) { endLatch = new CountDownLatch(workers.size()); - if(null == this.executorService || this.executorService.isShutdown()){ + if (null == this.executorService || this.executorService.isShutdown()) { this.executorService = ThreadUtil.newExecutor(threadSize); } for (Worker worker : workers) { @@ -150,11 +153,11 @@ public class SyncFinisher implements Closeable { * * @since 5.6.6 */ - public void stop(){ - if(null != this.executorService){ + public void stop() { + if (null != this.executorService) { this.executorService.shutdown(); + this.executorService = null; } - this.executorService = null; clearWorker(); } @@ -166,13 +169,13 @@ public class SyncFinisher implements Closeable { *
  • 执行start(false)后,用户自行判断结束点执行此方法
  • * * - * @since 5.6.6 + * @since 5.8.11 */ - public void stopNow(){ - if(null != this.executorService){ + public void stopNow() { + if (null != this.executorService) { this.executorService.shutdownNow(); + this.executorService = null; } - this.executorService = null; clearWorker(); } @@ -202,7 +205,6 @@ public class SyncFinisher implements Closeable { * 工作者,为一个线程 * * @author xiaoleilu - * */ public abstract class Worker implements Runnable {