'How to know which specific runnable has died in my threadPoolExecutor?
I am running a fixed amount of threads using newFixedThreadPool()
and need to be able to
- know when one of the runnables has died, and
- know which specific runnable was the one that died.
One solution for this was by wrapping the Runnables with as a Thread object and calling on isAlive()
. The Threads ran as they should have, but since aThread.start()
was never called by the executor always returned false
which is of no use to me. I considered the possibility of having the Runnables trigger a flag at the beginning of the run()
function just like this question's answer suggested. What would be the best way to keep track of the Runnables that have died? Because my intention is to submit a thread to the executor that that would essentially do the same thing as the one that died.
Solution 1:[1]
After looking at the documentation that @vk3105 provided I had an idea. I ended up looking at this and implemented Future future = executorService.submit(aRunnable)
so that i can check if that runnable was terminated or not by using future.isDone()
or if the Runnable was cancelled before it was completed future.isCancelled()
.
Solution 2:[2]
Maybe you can you use Thread.getState().
And here is a state diagram for threads. http://bighai.com/ppjava/?p=144
http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html#getState()
http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.State.html
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Salvador Hernandez |
Solution 2 | Glorfindel |