首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CountDownLatch InterruptedException

CountDownLatch InterruptedException
EN

Stack Overflow用户
提问于 2010-07-03 03:01:31
回答 2查看 7.4K关注 0票数 13

我正在使用CountDownLatch在两个线程之间同步初始化过程,我想知道如何正确处理它可能抛出的InterruptedException

我最初写的代码是这样的:

代码语言:javascript
复制
    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会在这里抛出,处理它的最佳实践是什么?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-07-03 03:08:05

这正是您不应该对InterruptedException执行的操作。InterruptedException基本上是对该线程终止的礼貌请求。线程应该尽快清理并退出。

关于这一点,IBM有一篇很好的文章:http://www.ibm.com/developerworks/java/library/j-jtp05236.html

这就是我要做的:

代码语言:javascript
复制
// Run while not interrupted.
while(!(Thread.interrupted())
{
    try
    {
        // Do whatever here.
    }
    catch(InterruptedException e)
    {
        // This will cause the current thread's interrupt flag to be set.
        Thread.currentThread().interrupt();
    }
}

// Perform cleanup and exit thread.

这样做的好处是:如果您的线程在阻塞方法中被中断,则不会设置中断位,而是抛出InterruptedException。如果您的线程在不处于阻塞方法时被中断,则将设置中断位,并且不会抛出任何异常。因此,通过调用interrupt()在异常上设置标志,这两种情况都被规范化为第一种情况,然后由循环条件检查。

作为额外的好处,这还允许您通过简单地中断线程来停止线程,而不是发明自己的机制或接口来设置某个布尔标志来执行完全相同的操作。

票数 11
EN

Stack Overflow用户

发布于 2010-07-03 03:37:08

如果您没有预见到Thread可能会被中断的任何合法原因,也想不出任何合理的反应,我会说您应该这样做

代码语言:javascript
复制
 catch (InterruptedException e){
      throw new AssertionError("Unexpected Interruption",e);
 }

这样,如果发生这种中断,应用程序将明显失败,从而更容易在测试过程中被发现。然后,您可以考虑应用程序应该如何处理这些问题,或者它们在设计中是否存在任何问题。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3168430

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档