summaryrefslogtreecommitdiffstats
path: root/src/main/java/derms/util/ThreadPool.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/derms/util/ThreadPool.java')
-rw-r--r--src/main/java/derms/util/ThreadPool.java21
1 files changed, 20 insertions, 1 deletions
diff --git a/src/main/java/derms/util/ThreadPool.java b/src/main/java/derms/util/ThreadPool.java
index 33588ff..ccd3afa 100644
--- a/src/main/java/derms/util/ThreadPool.java
+++ b/src/main/java/derms/util/ThreadPool.java
@@ -8,7 +8,26 @@ import java.util.logging.Logger;
public class ThreadPool {
public static final Duration timeout = Duration.ofSeconds(1);
- public static void shutDown(ExecutorService pool, Logger log) {
+ public static void shutdown(ExecutorService pool, Logger log) {
+ pool.shutdown();
+ try {
+ // Wait for existing threads to stop.
+ if (!pool.awaitTermination(timeout.toMillis(), TimeUnit.MILLISECONDS)) {
+ log.warning("Thread pool did not terminate after " + timeout + ". Forcefully shutting down...");
+ pool.shutdownNow(); // Cancel running tasks.
+ // Wait for tasks to stop.
+ if (!pool.awaitTermination(timeout.toMillis(), TimeUnit.MILLISECONDS))
+ log.warning("Thread pool did not terminate after " + timeout);
+ }
+ } catch (InterruptedException e) {
+ // (Re-)Cancel if current thread also interrupted.
+ pool.shutdownNow();
+ // Preserve interrupt status.
+ Thread.currentThread().interrupt();
+ }
+ }
+
+ public static void shutdownNow(ExecutorService pool, Logger log) {
pool.shutdownNow();
try {
if (!pool.awaitTermination(timeout.toMillis(), TimeUnit.MILLISECONDS))