public void gracefulShutdown ( final ExecutorService service , final Duration timeout ) throws InterruptedException { service . shutdown ( ) ; // Disable new tasks from being submitted final long timeout_in_unit_of_miliseconds = timeout . toMillis ( ) ; // Wait a while for existing tasks to terminate if ( ! service . awaitTermination ( timeout_in_unit_of_miliseconds , MILLI_SECONDS_TIME_UNIT ) ) { service . shutdownNow ( ) ; // Cancel currently executing tasks // Wait a while for tasks to respond to being cancelled if ( ! service . awaitTermination ( timeout_in_unit_of_miliseconds , MILLI_SECONDS_TIME_UNIT ) ) { logger . error ( "The executor service did not terminate." ) ; } } }
Gracefully shuts down the given executor service .