I am using the CountDownLatch to synchronize an initialization process between two threads and i was wondering about the proper handling of the InterruptedException that it might throw.
the code i initially wrote was this:
private CountDownLatch initWaitHandle = new CountDownLatch(1);
/**
* This method will block until the thread has fully initialized, this should only be called from different threads Ensure that the thread has started before this is called.
*/
public void ensureInitialized()
{
assert this.isAlive() : "The thread should be started before calling this method.";
assert Thread.currentThread() != this, "This should be called from a different thread (potential deadlock)";
while(true)
{
try
{
//we wait until the updater thread initializes the cache
//that way we know
initWaitHandle.await();
break;//if we get here the latch is zero and we are done
}
catch (InterruptedException e)
{
LOG.warn("Thread interrupted", e);
}
}
}
Does this pattern make sense? Basically is it a good idea to ignore the InterruptedException just keep waiting until it succeeds. I guess i just don't understand the situations under which this would get interrupted so i don't know if i should be handling them differently.
Why would an InterruptedException get thrown here, what is a best practise for handling it?