I want a thread in a Java program to loop until all other threads die, and then end the loop. How can I know when my loop thread is the only thread remaining?
In my situation, the loop thread will have no reference to any other threads, so I don't think isAlive()
helps me.
This might or might not help, depending on your use-case.
Set your loop thread to daemon mode
setDaemon(true);
and Java will kill it for you if all the non-daemon threads are gone.
Would this not be a situation where you would consider the Thread Pool Pattern?
If not, would it not be better to maintain a list of active threads, removing each as they are destroyed?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With