Java – stop all tasks in executorservice
I have some executor services to schedule local tasks, such as reading files, connecting to databases, etc These processes perform a lot of logging, which is based on the fact that many threads run concurrently and write their own things to the log
Now, an exception can be thrown at a certain point in time, which reaches the main method to catch all exceptions Then I shut down all services and cancel each task, hoping to block all further messages to the log Unfortunately, after closing everything, these messages still show up... What do you think?
Update: This is some 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); }
Solution
The task will continue to run until it reaches the point where it detects that the thread has been interrupted This can happen when some system or thread functions are called, and exceptions may be thrown In your case, you may need to check yourself by calling
Thread. currentThread(). isInterrupted()
It's a good idea if your code runs a loop and you want to stop in this way