我正在使用CountDownLatch來同步兩個線程之間的初始化過程,我想知道它可能拋出的InterruptedException的正確處理方法。CountDownLatch InterruptedException
我最初寫的代碼是這樣的:
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);
}
}
}
這種模式是否有意義?基本上忽略InterruptedException只是一直等到它成功是一個好主意。我想我只是不明白這種情況會中斷,所以我不知道我是否應該以不同的方式處理它們。
爲什麼一個InterruptedException在這裏得到拋出,什麼是處理它的最佳做法?
嘎我試圖修復你的格式,並做了編輯衝突。我沒有選擇回滾。抱歉! – 2010-07-02 19:18:00
我已經回滾了。感謝您的幫助!我注意到它是錯誤的,但決定在寫下文字牆的同時修復它。 – jdmichal 2010-07-02 19:19:19
實際上不是,我只是真正習慣了維基百科,在那裏你點擊回滾你想撤消的編輯,而不是你想要回滾的修訂**到**。 – 2010-07-02 19:19:31