gpt4 book ai didi

java - 在循环中调用可中断的阻塞方法

转载 作者:行者123 更新时间:2023-11-30 07:15:40 25 4
gpt4 key购买 nike

这个问题来自 Brian Goetz 所著的《Java 并发实践》一书的一个例子,第 7 章,7.1.3 响应中断(第 143 - 144 页)书上说

Activities that do not support cancellation but still call interruptible blocking methods will have to call them in a loop, retrying when interruption is detected. In this case, they should save the interruption status locally and restore it just before returning as shown in example below, rather than immediately upon catching InterruptedException. Setting the interrupted status too ealry could result in an infinite loop, because most interruptible blocking methods check the interrupted status on entry and throw InterruptedException immediately if it is set......

    public Task getNextTask(BlockingQueue<Task> queue) {
boolean interrupted = false;
try {
while (true) {
try {
return queue.take();
} catch (InterruptedException e) {
interrrupted = true;
}
}
} finally {
if (interrupted)
Thread.currentThread().interrupt();
}
}

我的问题是为什么需要循环?

此外,如果 queue.take() 抛出 interruptedException,那么我假设中断标志设置在当前线程上是否正确?那么下一次调用 queue.take() 将再次抛出 interruptedException,因为当前线程上一个中断没有被清除,这不会导致无限循环吗?

最佳答案

回答你的第一个问题

My question is why is the loop required?

在这一行

       Activities that do not support cancellation but still call interruptible 
blocking methods will have to call them in a loop, retrying when interruption
is detected.
From Java concurrency in Practice 7.1.3

getNextTask 方法不支持取消。即即使线程被中断它也不会取消它的任务并会重试。请注意,getNextTask 方法正在调用一个可中断的阻塞方法 queue.take(),它会抛出 intteruptedException。 getNextTask 方法必须处理 interruptedException,因为它不支持取消,应该重试。简而言之,它是一种方法策略,决定是重试还是只在方法签名中抛出中断的异常。

你的第二个问题

Also if queue.take() throws an interruptedException then I am assuming the 
interrupt flag is set on the current thread correct?

否 如果抛出中断异常则重置中断标志。

还要注意finally block 中,线程是自中断的(Thread.currentThread().interrupt()),因为getNextTask的调用栈还可能调用其他可中断的阻塞方法,此类方法一般先检查当前是否线程是否被中断。如果是,则中断标志被重置并且中断异常像下面这样被抛出。

Below is the Code from AQS(AbstractQueueSynchronizer) from java

public final void acquireInterruptibly(int arg) throws InterruptedException {
if (Thread.interrupted())
throw new InterruptedException();
if (!tryAcquire(arg))
doAcquireInterruptibly(arg);
}

关于java - 在循环中调用可中断的阻塞方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17549620/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com