I am running a highly concurrent Java program.
While many threads are submitting tasks to an executor service, at a certain point the main thread invokes ExecutorService.shutdownNow()
.
After this action, I would expect that:
- No additional task would be accepted by the executor service
- The executor service's queue is clear
- The remaining running workers are interrupted, that is, at a certain point they shut down if they correctly manage the InterruptedException and / or explicitly check
Thread.currentThread().isInterrupted()
Since I am in a situation where:
- The executor service is "shutting down" due to an
ExecutorService.shutdownNow()
, but is not shut down, that is,ExecutorService.awaitTermination(long, TimeUnit)
never returnstrue
- There are some pending threads managed by my executor service waiting on a
BlockingQueue.take()
- If I invoke again
ExecutorService.shutdownNow()
, the pending threads die byInterruptedException
onBlockingQueue.take()
I suppose that the interruption was received by those threads before invoking BlockingQueue.take()
and the InterruptedException was ignored.
I was also wondering if the ExecutorService.shutdownNow()
is thread safe, that is, it works correctly even if the thread pool is receiving many submissions.
All in all, I would have two questions:
- Is there any alternative scenario that would explain my situation?
- Is it possible that
ExecutorService.shutdownNow()
is not thread safe?