How do I run a thread in java that lets me know when another thread has died?

Assume that I have a thread A in java. This thread continues to perform some task A. I have another thread B that must perform a task B only after Task A is finished. How do I implement this in Java?

Jon Skeet
people
quotationmark

You can use Thread.join() to basically block one thread until another thread terminates:

// In thread B
threadA.join();
doStuff();

Note that this won't work properly if you use a thread pool e.g. via an executor service. If you need to do that (and I'd generally recommend using executors instead of "raw" threads) you'd need to make the executor task notify any listeners that it has completed (e.g. via a CountDownLatch).

If you use Guava, you should look at ListenableFuture, too, which simplifies things.

people

See more on this question at Stackoverflow