Java - stop all tasks in ExecutorService

I have several executor services that schedule local tasks, such as reading a file, connecting to db, etc. These processes make a huge amount of logging, which is extensively based on the fact that there are many threads working simultaneously in the log.

Now, at some point in time, an exception can be raised that reaches the main method, where all exceptions are caught. Then I close all services and cancel each task, hoping to prevent all subsequent log messages. Unfortunately, messages still appear after I closed everything ... Any ideas?

UPDATE: Here is the code

public class Scheduler{ private final ExecutorService service; private final ConcurrentMap<Object, Future<V>> cache; ... public void shutDown() { service.shutdownNow(); for (Future task : cache.values()) task.cancel(true); } 
+6
source share
4 answers

The task will continue until it reaches the point where it detects that the thread has been interrupted. This can happen when calling some System or Thread functions, and you may get an exception. In your case, you probably need to test yourself by calling

Thread.currentThread().isInterrupted()

It is a good idea to do this if your code starts loops and you expect to stop it this way.

+7
source

When you shutdownNow your executor or call cancel(true) (by the way, shutdownNow already cancels already sent tasks, so your loop is not needed) your tasks are interrupted.

Depending on how they respond to the interrupt, they may:

  • stop what they do immediately.
  • stop what they do after a while because the interrupt signal is not checked regularly enough
  • keep doing what they do because the interrupt signal was ignored

For example, if your tasks run a while(true) , you can replace it with something like:

 while(!Thread.currentThread().isInterrupted()) { //your code here } cleanup(); //and exit 

Another example:

 for (int i = 0; i < aBigNumber; i++) { if (Thread.currentThread().isInterrupted()) { break; } //rest of the code for the loop } cleanup(); //and exit 

Another example, if you call a method that throws an InterruptedException:

 try { Thread.sleep(forever); //or some blocking IO or file reading... } catch (InterruptedException e) { cleanup(); Thread.currentThread.interrupt(); //and exit } 
+5
source

Artists support 2 completion approaches

shutdown () : initiates an orderly shutdown in which previously assigned tasks are executed, but new tasks will not be accepted. A call has no additional effect if it is already turned off.

shutdownNow () : tries to stop all active task execution, stops processing pending tasks and returns a list of tasks waiting to be completed. There is no guarantee other than trying to maximize the effort to stop processing that is actively performing tasks.

Link: http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/ExecutorService.html#shutdownNow ()

+4
source

- Try using the shutdowNow() method, it will turn off the entire task launched by this Executor, throwing an InterruptedException , but IO and Synchronized operation cannot be interrupted.

For instance:

 ExecutorService executor = Executors.newFixedThreadPool(); executor.execute(); ... ... executor.shutdownNow(); 

- The cancel(true) method can be used with the submit() method to turn off a specific task.

0
source

All Articles