diff --git a/half-sync-half-async/src/main/java/com/iluwatar/halfsynchalfasync/App.java b/half-sync-half-async/src/main/java/com/iluwatar/halfsynchalfasync/App.java index 90cb5ecbf..b24e29b6a 100644 --- a/half-sync-half-async/src/main/java/com/iluwatar/halfsynchalfasync/App.java +++ b/half-sync-half-async/src/main/java/com/iluwatar/halfsynchalfasync/App.java @@ -94,6 +94,8 @@ public class App { service.execute(new ArithmeticSumTask(500)); service.execute(new ArithmeticSumTask(2000)); service.execute(new ArithmeticSumTask(1)); + + service.close(); } /** diff --git a/half-sync-half-async/src/main/java/com/iluwatar/halfsynchalfasync/AsynchronousService.java b/half-sync-half-async/src/main/java/com/iluwatar/halfsynchalfasync/AsynchronousService.java index 4e2f9c23a..3e64edd98 100644 --- a/half-sync-half-async/src/main/java/com/iluwatar/halfsynchalfasync/AsynchronousService.java +++ b/half-sync-half-async/src/main/java/com/iluwatar/halfsynchalfasync/AsynchronousService.java @@ -22,6 +22,9 @@ */ package com.iluwatar.halfsynchalfasync; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.util.concurrent.BlockingQueue; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; @@ -38,6 +41,7 @@ import java.util.concurrent.TimeUnit; */ public class AsynchronousService { + private static final Logger LOGGER = LoggerFactory.getLogger(AsynchronousService.class); /* * This represents the queuing layer as well as synchronous layer of the pattern. The thread pool * contains worker threads which execute the tasks in blocking/synchronous manner. Long running @@ -95,4 +99,16 @@ public class AsynchronousService { } }); } + + /** + * Stops the pool of workers. This is a blocking call to wait for all tasks to be completed. + */ + public void close() { + service.shutdown(); + try { + service.awaitTermination(10, TimeUnit.SECONDS); + } catch (InterruptedException ie) { + LOGGER.error("Error waiting for executor service shutdown!"); + } + } }