From b82496befd17a9b4fbf956eeab4d85bba498a940 Mon Sep 17 00:00:00 2001 From: Looly Date: Sat, 26 Nov 2022 10:58:32 +0800 Subject: [PATCH] fix code --- .../cn/hutool/core/thread/SyncFinisher.java | 36 ++++++++++++++----- 1 file changed, 28 insertions(+), 8 deletions(-) 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 ae49c0e22..ebdee040a 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(final 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 (final Worker worker : workers) { @@ -150,9 +153,27 @@ public class SyncFinisher implements Closeable { * * @since 5.6.6 */ - public void stop(){ - if(null != this.executorService){ - this.executorService.shutdown(); + public void stop() { + stop(false); + } + + /** + * 结束线程池。此方法执行两种情况: + *
    + *
  1. 执行start(true)后,调用此方法结束线程池回收资源
  2. + *
  3. 执行start(false)后,用户自行判断结束点执行此方法
  4. + *
+ * + * @param isStopNow 是否立即结束所有线程(包括正在执行的) + * @since 5.6.6 + */ + public void stop(final boolean isStopNow) { + if (null != this.executorService) { + if (isStopNow) { + this.executorService.shutdownNow(); + } else { + this.executorService.shutdown(); + } } this.executorService = null; @@ -184,7 +205,6 @@ public class SyncFinisher implements Closeable { * 工作者,为一个线程 * * @author xiaoleilu - * */ public abstract class Worker implements Runnable {