public void doRecovery ( ) { // Attempts to stop all actively executing tasks, halts the // processing of waiting tasks, and returns a list of the tasks // that were awaiting execution. // // This method does not wait for actively executing tasks to // terminate. // // There are no guarantees beyond best-effort attempts to stop // processing actively executing tasks. For example, typical // implementations will cancel via {@link Thread#interrupt}, so any // task that fails to respond to interrupts may never terminate. try { synchronized ( this ) { executorService . shutdownNow ( ) ; //shutting down current running threads threadPoolVersion ++ ; //updating the thread pool version to a new one - so current running threads will exit logger . warn ( "Worker is in doRecovery(). Cleaning state and cancelling running tasks. It may take up to 30 seconds..." ) ; } boolean finished = executorService . awaitTermination ( 30 , TimeUnit . SECONDS ) ; if ( finished ) { logger . warn ( "Worker succeeded to cancel running tasks during doRecovery()." ) ; } else { logger . warn ( "Not all running tasks responded to cancel." ) ; } } catch ( InterruptedException ex ) { /*ignore*/ } mapOfRunningTasks . clear ( ) ; //Make new executor executorService = new ThreadPoolExecutor ( numberOfThreads , numberOfThreads , Long . MAX_VALUE , TimeUnit . NANOSECONDS , inBuffer , new WorkerThreadFactory ( ( threadPoolVersion ) + "_WorkerExecutionThread" ) ) ; }
Must clean the buffer that holds Runnables that wait for execution and also drop all the executions that currently run