i want to stop all the submitted tasks on an ExecutorService but i couldn't find a method to do it. any suggestions ?
for example, var pool is an ExecutorService.
//start a task
pool.execute(task);
//
try {
pool.shutdown();
if(!pool.awaitTermination(awaitTime, TimeUnit.MILLISECONDS)){
pool.shutdownNow();
}
} catch (InterruptedException e) {
pool.shutdownNow();
}
A deterministic shutdown for any task submitted to ExecutorService
is possible only when the task itself is designed to respond to a external signal ( such as an Interruption on the thread running the task , or some other application designed flag ). ExecutorService
itself only helps in as much as it will raise these signals for you ( when you call its shutdown()
and shutdownNow()
methods ) and notify all running tasks. If you want to be able to shutdown all tasks ( including the ones which are already running and not yet finished ) then design these tasks to have a cancellation policy.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.