From 21935fbde6ee26a6694b7887da92e9174541c62c Mon Sep 17 00:00:00 2001 From: Michiel Meeuwissen Date: Fri, 1 Nov 2024 12:00:20 +0100 Subject: [PATCH] Common pool cannot be shut down. --- .../java/nl/vpro/util/ThreadPoolShutdowner.java | 14 ++++++++++---- .../src/main/java/nl/vpro/util/ThreadPools.java | 5 ++--- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/vpro-shared-util/src/main/java/nl/vpro/util/ThreadPoolShutdowner.java b/vpro-shared-util/src/main/java/nl/vpro/util/ThreadPoolShutdowner.java index ff3578b26..a2b6d0e16 100644 --- a/vpro-shared-util/src/main/java/nl/vpro/util/ThreadPoolShutdowner.java +++ b/vpro-shared-util/src/main/java/nl/vpro/util/ThreadPoolShutdowner.java @@ -1,22 +1,28 @@ package nl.vpro.util; import lombok.extern.log4j.Log4j2; -import org.junit.platform.launcher.TestExecutionListener; -import org.junit.platform.launcher.TestPlan; import java.util.concurrent.ForkJoinPool; +import java.util.concurrent.TimeUnit; + +import org.junit.platform.launcher.TestExecutionListener; +import org.junit.platform.launcher.TestPlan; /** * A junit TestExecutionListener that just makes sure that all ThreadPools are shutdown after the tests. + * Also, it calls {@link ForkJoinPool#awaitQuiescence(long, TimeUnit)} (100s) on {@link ForkJoinPool#commonPool()} */ @Log4j2 public class ThreadPoolShutdowner implements TestExecutionListener { @Override public void testPlanExecutionFinished(TestPlan testPlan) { + ThreadPools.shutdown(); - log.info("Shutting down ForkJoinPool.commonPool() too"); - ForkJoinPool.commonPool().shutdown(); + log.info("Waiting for ForkJoinPool.commonPool() too"); + // commonPool cannot be shut down. But you can wait for tasks + boolean b = ForkJoinPool.commonPool().awaitQuiescence(100, TimeUnit.SECONDS); + } } diff --git a/vpro-shared-util/src/main/java/nl/vpro/util/ThreadPools.java b/vpro-shared-util/src/main/java/nl/vpro/util/ThreadPools.java index a0521d4aa..da44bc7e9 100644 --- a/vpro-shared-util/src/main/java/nl/vpro/util/ThreadPools.java +++ b/vpro-shared-util/src/main/java/nl/vpro/util/ThreadPools.java @@ -94,13 +94,12 @@ public Thread newThread(@NonNull Runnable r) { false, Thread.NORM_PRIORITY)); - - public static void shutdown() { + public static void shutdown() { log.info("Shutting down thread pools"); copyExecutor.shutdown(); startUpExecutor.shutdown(); backgroundExecutor.shutdown(); longBackgroundExecutor.shutdown(); - } + } }