@ Beta @ CanIgnoreReturnValue @ GwtIncompatible // concurrency @ SuppressWarnings ( "GoodTime" ) // should accept a java.time.Duration public static boolean shutdownAndAwaitTermination ( ExecutorService service , long timeout , TimeUnit unit ) { long halfTimeoutNanos = unit . toNanos ( timeout ) / 2 ; // Disable new tasks from being submitted service . shutdown ( ) ; try { // Wait for half the duration of the timeout for existing tasks to terminate if ( ! service . awaitTermination ( halfTimeoutNanos , TimeUnit . NANOSECONDS ) ) { // Cancel currently executing tasks service . shutdownNow ( ) ; // Wait the other half of the timeout for tasks to respond to being cancelled service . awaitTermination ( halfTimeoutNanos , TimeUnit . NANOSECONDS ) ; } } catch ( InterruptedException ie ) { // Preserve interrupt status Thread . currentThread ( ) . interrupt ( ) ; // (Re-)Cancel if current thread also interrupted service . shutdownNow ( ) ; } return service . isTerminated ( ) ; }
Shuts down the given executor service gradually first disabling new submissions and later if necessary cancelling remaining tasks .