This commit is contained in:
Looly 2022-11-26 10:58:32 +08:00
parent 3938b2b97c
commit b82496befd

View File

@ -23,7 +23,6 @@ import java.util.concurrent.ExecutorService;
* sf.start() * sf.start()
* </pre> * </pre>
* *
*
* @author Looly * @author Looly
* @since 4.1.15 * @since 4.1.15
*/ */
@ -34,9 +33,13 @@ public class SyncFinisher implements Closeable {
private ExecutorService executorService; private ExecutorService executorService;
private boolean isBeginAtSameTime; private boolean isBeginAtSameTime;
/** 启动同步器用于保证所有worker线程同时开始 */ /**
* 启动同步器用于保证所有worker线程同时开始
*/
private final CountDownLatch beginLatch; private final CountDownLatch beginLatch;
/** 结束同步器用于等待所有worker线程同时结束 */ /**
* 结束同步器用于等待所有worker线程同时结束
*/
private CountDownLatch endLatch; private CountDownLatch endLatch;
/** /**
@ -123,7 +126,7 @@ public class SyncFinisher implements Closeable {
public void start(final boolean sync) { public void start(final boolean sync) {
endLatch = new CountDownLatch(workers.size()); endLatch = new CountDownLatch(workers.size());
if(null == this.executorService || this.executorService.isShutdown()){ if (null == this.executorService || this.executorService.isShutdown()) {
this.executorService = ThreadUtil.newExecutor(threadSize); this.executorService = ThreadUtil.newExecutor(threadSize);
} }
for (final Worker worker : workers) { for (final Worker worker : workers) {
@ -150,9 +153,27 @@ public class SyncFinisher implements Closeable {
* *
* @since 5.6.6 * @since 5.6.6
*/ */
public void stop(){ public void stop() {
if(null != this.executorService){ stop(false);
this.executorService.shutdown(); }
/**
* 结束线程池此方法执行两种情况
* <ol>
* <li>执行start(true)调用此方法结束线程池回收资源</li>
* <li>执行start(false)用户自行判断结束点执行此方法</li>
* </ol>
*
* @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; this.executorService = null;
@ -184,7 +205,6 @@ public class SyncFinisher implements Closeable {
* 工作者为一个线程 * 工作者为一个线程
* *
* @author xiaoleilu * @author xiaoleilu
*
*/ */
public abstract class Worker implements Runnable { public abstract class Worker implements Runnable {