我的假设是,LockSupport.parkNanos(long)
不会抛出InterruptedException
,但标志可以线程上设置。调用LockSupport.parkNanos(long)后是否需要检查线程中断状态?
- 这是正确的吗?
- 如果是这样,我需要检查标志,并抛出
InterruptedException
?
使用范例:
import java.util.concurrent.locks.LockSupport;
public void parkNanosInterruptibly(final long nanos)
throws InterruptedException {
LockSupport.parkNanos(nanos);
// If this thread was interrupted during parkNanos(), we must throw "by contract".
if (Thread.interrupted()) {
throw new InterruptedException();
}
}