0

Is there a way to interrupt all active tasks in an Executor? I don't believe I must got all previous submited tasks stored in order to fire such a common operation. What if I am using CompletableFutures, which have no control on the computation code as Future does, do I really need to mess with complete() synchronization when I can simply tell the executor to do it for me?

I look for something asynch like:

 //Easier than keeping a collection of tasks by both methods 
 //in a global var, which needs to parse which ones are 
 //already executed, during execution or whatever

 public void start(MyTask task){
    executorService.submit(task);
 }

 public void stop(){
   executorService.cancelAnyTask();  //This or
   executorService.interruptAnyActiveTask(); //Even this
 }

EDIT: I want to interrupt (or either cancel) active tasks, I don't mind the queued ones (really does not mind if the queued are discarded or not). I just look for clearing the executor work at a given discrete time, even if 1 ms later it starts to execute queued tasks again.

Whimusical
  • 6,401
  • 11
  • 62
  • 105
  • Possible duplicate of [Removing all queued tasks of an ThreadPoolExecutor](http://stackoverflow.com/questions/1679579/removing-all-queued-tasks-of-an-threadpoolexecutor) – Tunaki Nov 29 '15 at 14:00
  • What is the actual problem you're trying to solve? – the8472 Nov 29 '15 at 14:07
  • @Tunaki, he says interrupting, that could concern already-running tasks, not just queued ones. – the8472 Nov 29 '15 at 14:08
  • @the8472 True but I think the accepted answer still applies. – Tunaki Nov 29 '15 at 14:09
  • I don't mind the queued tasks, I just want to tell executor to interupt active tasks and keep proceeding as usual. If I need to deal with futures, I will run into synch problems, as telling which ones are completed and so on. I feel like chasing the internal state of executor. It must be a way to tell executor directly to interrupt current active tasks without shutdowNow it forever! – Whimusical Nov 29 '15 at 17:22

0 Answers0