在阅读CountDownLatch.await()方法时看到如下代码:
public void await() throws InterruptedException {
sync.acquireSharedInterruptibly(1);
}
public final void acquireSharedInterruptibly(int arg)
throws InterruptedException {
if (Thread.interrupted()) //问题点
throw new InterruptedException();
if (tryAcquireShared(arg) < 0)
doAcquireSharedInterruptibly(arg);
}
当前线程被打断时,调用await()方法将会抛出InterruptedException异常。
测试代码:
public static void main(String[] args) throws InterruptedException {
CountDownLatch countDownLatch = new CountDownLatch(10);
Thread.currentThread().interrupt();
countDownLatch.await();
}
运行程序抛出InterruptedException异常,是符合预期的。但是当BUBUG查看Thread.interrupted()执行结果时,却发现返回结果为false,但是实际值应该是true。
在Eclipse测试时,情况和idea是一样的,这就很费解,目前没有明白是什么原因...,先记录下。
图1
网友评论