CountDownLatch InterruptedException [英] CountDownLatch InterruptedException

查看:402
本文介绍了CountDownLatch InterruptedException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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);
            }
        }
    }

这个模式是否有意义?基本上,忽略 InterruptedException 只要保持等待,直到成功。我想我只是不明白这种情况会被中断,所以我不知道我应该如何处理它们不同。

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.

为什么InterruptedException获取扔在这里,处理它的最佳做法是什么?

Why would an InterruptedException get thrown here, what is the best practice for handling it?

推荐答案

这是你不应该为 InterruptedException的。一个 InterruptedException 基本上是一个有礼貌的请求,该线程终止。线程应该尽快清理并退出。

This is exactly what you should not do for an InterruptedException. An InterruptedException is basically a polite request for that thread to terminate. The thread should clean up and exit as soon as possible.

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

IBM has a good article posted about this: http://www.ibm.com/developerworks/java/library/j-jtp05236.html

这是我会做的:

// 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()来设置异常的标志,这两种情况都是标准化到第一种情况,然后通过循环条件进行检查。

The advantage to doing it this way is thus: If your thread is interrupted while in a blocking method, the interrupt bit will not be set and an InterruptedException is thrown instead. If your thread is interrupted while not in a blocking method, the interrupted bit will be set, and no exception will be thrown. So by calling interrupt() to set the flag on an exception, both cases are being normalized to the first case, which is then checked by the loop conditional.

作为一个额外的好处,这也可以通过简单地中断它来阻止你的线程,而不是inven你自己的机制或接口设置一些布尔标志来做同样的事情。

As an added bonus, this also lets you stop your thread by simply interrupting it, instead of inventing your own mechanism or interface to set some boolean flag to do the exact same thing.

这篇关于CountDownLatch InterruptedException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆