4242 */
4343@ ThreadSafe
4444@ ToString
45- public final class ConseqExecutor implements SequentialExecutor {
45+ public final class ConseqExecutor implements SequentialExecutor , AutoCloseable {
4646
4747 private final Map <Object , CompletableFuture <?>> activeSequentialTasks = new ConcurrentHashMap <>();
4848 private final ExecutorService adminService = Executors .newSingleThreadExecutor ();
@@ -52,7 +52,6 @@ public final class ConseqExecutor implements SequentialExecutor {
5252 * max parallelism of task execution.
5353 */
5454 private final ExecutorService workerExecutorService ;
55- private final ConditionFactory await = Awaitility .await ().forever ();
5655
5756 private ConseqExecutor (ExecutorService workerExecutorService ) {
5857 this .workerExecutorService = workerExecutorService ;
@@ -83,6 +82,10 @@ private ConseqExecutor(ExecutorService workerExecutorService) {
8382 return new ConseqExecutor (workerExecutorService );
8483 }
8584
85+ private static ConditionFactory await () {
86+ return Awaitility .await ().forever ();
87+ }
88+
8689 private static <T > T call (Callable <T > task ) {
8790 try {
8891 return task .call ();
@@ -156,7 +159,7 @@ public <T> CompletableFuture<T> submit(@NonNull Callable<T> task, @NonNull Objec
156159 public void shutdown () {
157160 new Thread (() -> {
158161 workerExecutorService .shutdown ();
159- await .until (activeSequentialTasks ::isEmpty );
162+ await () .until (activeSequentialTasks ::isEmpty );
160163 adminService .shutdown ();
161164 }).start ();
162165 }
@@ -173,6 +176,14 @@ public boolean isTerminated() {
173176 return neverStartedTasks ;
174177 }
175178
179+ @ Override
180+ public void close () {
181+ workerExecutorService .shutdown ();
182+ await ().until (activeSequentialTasks ::isEmpty );
183+ adminService .shutdown ();
184+ await ().until (this ::isTerminated );
185+ }
186+
176187 int estimateActiveExecutorCount () {
177188 return activeSequentialTasks .size ();
178189 }
0 commit comments