Search code examples
javamultithreadingwaitinterrupt

How should be InterruptedException handled for CountDownLatch.wait


How should be InterruptedException be handled during waiting of CountDownLatch.wait?

I need to be sure that CountDownLatch.wait will wait all other thread to be finished, but if during waiting exception will be thrown, how should i deal with it?


Solution

  • First of all, the method is await(), not wait(). wait() is a method from java.lang.Object, which does something completely different.

    You won't get any InterruptedException unless you decide, from another thread, to interrupt the thread being blocked in latch.await() (by calling thread.interrupt()). And since you decide to interrupt the thread, you should know what to do when the thread is interrupted. Usually, you decide to interrupt a thread when you want it to finish what he's doing as soon as possible, and stop running.