@Test public void testCleanShutdown() { Logger logger = LoggerFactory.getLogger("testCleanShutdown"); ExecutorService executor = Executors.newSingleThreadExecutor(); final CountDownLatch latch = new CountDownLatch(1); runWorker(latch, false, executor); Thread t = new Thread(() -> { try { Thread.sleep(200); latch.countDown(); } catch (InterruptedException e) { Assert.fail(); } }); t.start(); Assert.assertTrue(ThreadUtils.shutdownExecutor(executor, Duration.ofMinutes(1), logger)); }
public static boolean shutdownExecutor(ExecutorService executor, Duration timeout, Logger logger) { executor.shutdown(); try { if (!executor.awaitTermination(timeout.toMillis(), TimeUnit.MILLISECONDS)) { logger.warn("Executor service did not terminate gracefully within {}, force terminating ...", timeout); executor.shutdownNow(); if (!executor.awaitTermination(timeout.toMillis(), TimeUnit.MILLISECONDS)) { logger.error("Executor service failed to terminate (forced), worker threads might be swallowing " + "InterruptedException, giving up..."); } } } catch (InterruptedException ie) { logger.warn("Interrupted while waiting for executor service to terminate gracefully, force terminating ..."); executor.shutdownNow(); Thread.currentThread().interrupt(); } return executor.isTerminated(); }
ThreadUtils { public static boolean shutdownExecutor(ExecutorService executor, Duration timeout, Logger logger) { executor.shutdown(); try { if (!executor.awaitTermination(timeout.toMillis(), TimeUnit.MILLISECONDS)) { logger.warn("Executor service did not terminate gracefully within {}, force terminating ...", timeout); executor.shutdownNow(); if (!executor.awaitTermination(timeout.toMillis(), TimeUnit.MILLISECONDS)) { logger.error("Executor service failed to terminate (forced), worker threads might be swallowing " + "InterruptedException, giving up..."); } } } catch (InterruptedException ie) { logger.warn("Interrupted while waiting for executor service to terminate gracefully, force terminating ..."); executor.shutdownNow(); Thread.currentThread().interrupt(); } return executor.isTerminated(); } }
ThreadUtils { public static boolean shutdownExecutor(ExecutorService executor, Duration timeout, Logger logger) { executor.shutdown(); try { if (!executor.awaitTermination(timeout.toMillis(), TimeUnit.MILLISECONDS)) { logger.warn("Executor service did not terminate gracefully within {}, force terminating ...", timeout); executor.shutdownNow(); if (!executor.awaitTermination(timeout.toMillis(), TimeUnit.MILLISECONDS)) { logger.error("Executor service failed to terminate (forced), worker threads might be swallowing " + "InterruptedException, giving up..."); } } } catch (InterruptedException ie) { logger.warn("Interrupted while waiting for executor service to terminate gracefully, force terminating ..."); executor.shutdownNow(); Thread.currentThread().interrupt(); } return executor.isTerminated(); }  }
ThreadUtils { public static boolean shutdownExecutor(ExecutorService executor, Duration timeout, Logger logger) { executor.shutdown(); try { if (!executor.awaitTermination(timeout.toMillis(), TimeUnit.MILLISECONDS)) { logger.warn("Executor service did not terminate gracefully within {}, force terminating ...", timeout); executor.shutdownNow(); if (!executor.awaitTermination(timeout.toMillis(), TimeUnit.MILLISECONDS)) { logger.error("Executor service failed to terminate (forced), worker threads might be swallowing " + "InterruptedException, giving up..."); } } } catch (InterruptedException ie) { logger.warn("Interrupted while waiting for executor service to terminate gracefully, force terminating ..."); executor.shutdownNow(); Thread.currentThread().interrupt(); } return executor.isTerminated(); }  static boolean shutdownExecutor(ExecutorService executor, Duration timeout, Logger logger); }
ThreadUtils { public static boolean shutdownExecutor(ExecutorService executor, Duration timeout, Logger logger) { executor.shutdown(); try { if (!executor.awaitTermination(timeout.toMillis(), TimeUnit.MILLISECONDS)) { logger.warn("Executor service did not terminate gracefully within {}, force terminating ...", timeout); executor.shutdownNow(); if (!executor.awaitTermination(timeout.toMillis(), TimeUnit.MILLISECONDS)) { logger.error("Executor service failed to terminate (forced), worker threads might be swallowing " + "InterruptedException, giving up..."); } } } catch (InterruptedException ie) { logger.warn("Interrupted while waiting for executor service to terminate gracefully, force terminating ..."); executor.shutdownNow(); Thread.currentThread().interrupt(); } return executor.isTerminated(); }  static boolean shutdownExecutor(ExecutorService executor, Duration timeout, Logger logger);  }