Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 没有倒计时到零的倒计时锁存器如何在不被中断的情况下返回?_Java_Java.util.concurrent - Fatal编程技术网

Java 没有倒计时到零的倒计时锁存器如何在不被中断的情况下返回?

Java 没有倒计时到零的倒计时锁存器如何在不被中断的情况下返回?,java,java.util.concurrent,Java,Java.util.concurrent,(对于后代来说,我熟悉另一个问题,它的答案似乎表明我所观察到的情况是不可能的:) 我有一个CountDownLatch,它是用int参数1创建的。故意不在此闩锁上调用countDown() 我有一个ShutdownHook,它中断调用myLatch.await()的线程,还有一个catch块,它处理后续的InterruptedException 我观察到,当我的关闭挂钩被调用时,闩锁会正常“唤醒”。也就是说,await()方法返回,线程未中断,其中断状态(由isInterrupted()报告)为

(对于后代来说,我熟悉另一个问题,它的答案似乎表明我所观察到的情况是不可能的:)

我有一个
CountDownLatch
,它是用
int
参数
1
创建的。故意不在此闩锁上调用
countDown()

我有一个
ShutdownHook
,它中断调用
myLatch.await()
的线程,还有一个catch块,它处理后续的
InterruptedException

我观察到,当我的关闭挂钩被调用时,闩锁会正常“唤醒”。也就是说,
await()
方法返回,线程未中断,其中断状态(由
isInterrupted()
报告)为
false

我从
CountDownLatch
文档中了解到,这种情况是不可能的。我错过了什么?

等待闩锁的代码如下所示:

try {
    myLatch.await();
    System.out.println("*** done via unblock");
} catch (final InterruptedException interruptedException) {
    Thread.currentThread().interrupt();
    System.out.println("*** done via interrupt");
}
// t is the thread that is doing the await() call above
final Thread t = Thread.currentThread();
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
      t.interrupt();
      try {
          t.join();
      } catch (final InterruptedException interruptedException) {
        Thread.currentThread().interrupt();
      }
 }));
当我CTRL-C我的应用程序时,我看到,
***通过解除阻止
完成。我对文档的理解是,这是不可能的,因为
CountDownLatch
实例不会受到虚假唤醒的影响

关机挂钩代码如下所示:

try {
    myLatch.await();
    System.out.println("*** done via unblock");
} catch (final InterruptedException interruptedException) {
    Thread.currentThread().interrupt();
    System.out.println("*** done via interrupt");
}
// t is the thread that is doing the await() call above
final Thread t = Thread.currentThread();
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
      t.interrupt();
      try {
          t.join();
      } catch (final InterruptedException interruptedException) {
        Thread.currentThread().interrupt();
      }
 }));

谢天谢地,我已经找到了一个代码路径,在这个代码路径中,几个类之间确实存在着一些问题。谢天谢地,因为(a)我的代码现在可以工作了,(b)文档是正确的,虚假的唤醒实际上不是一件事™ 当我们谈论CountDownLatch#await()

时,从阅读CountDownLatch的
await()
方法的javadoc中可以看出,被清除的中断状态是有意义的。谢谢您的评论<代码>InterruptedException不会被抛出。您可以发布演示代码吗?我在JDK 11上使用了您的代码片段,当我按住Ctrl+C键时,我将通过中断完成
***。另外,您使用的是什么JDK?谢谢您的评论。JDK 8构建,呃,检查notes 181。在Mac电脑上。除了上面的摘录,我现在无法发布我的演示代码。我从你的评论中得知,我没有遗漏任何明显的东西。@lairdelson说实话,我还没有尝试过其他任何东西。只是试着运行它。现在在JDK8(u201)上试用,结果相同。我的测试代码很简单-只是一个主要方法,它定义了一个
新的CountDownLatch(1)
,然后运行shutdownHook代码段,然后运行wait代码段。