英文:
LockSupport parkUntil with blocker
问题
我正在尝试理解方法 `LockSupport::parkUntil` 的参数用途是什么。让我通过一个示例来说明我的意思:
public static void main(String[] args) {
Object blockedOn = new Object();
System.out.println(blockedOn.hashCode());
Thread parked = new Thread(() -> {
System.out.println("parking the thread");
long howMuch = System.currentTimeMillis() + 5 * 1000;
while (System.currentTimeMillis() < howMuch) {
LockSupport.parkUntil(blockedOn, howMuch);
}
System.out.println("parked the thread");
});
parked.start();
sleepOneSecond();
Object on = LockSupport.getBlocker(parked);
System.out.println(on.hashCode());
}
private static void sleepOneSecond() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
关于这段代码,实际上有一些问题,如果有人想帮助我理解的话。首先,就是那个 while
循环,我没有看到任何其他方式可以摆脱它(来自 LockSupport::parkUntil
的文档):
调用可能(也就是毫无理由地)会返回。
因此,我只能假设 LockSupport::parkUntil
可能会无故失败,因此我被迫将其放入循环中。
我接下来的问题是,blockedOn
参数的目的是什么?我理解,当等待足够长的时间后,我可以这样做:
Object on = LockSupport.getBlocker(parked);
也就是找出我被阻塞在哪个对象上的 当前 状态,然后根据这个状态做出相应的反应。我在考虑是否可以中断被阻塞的线程(当然它必须支持中断),但还有其他可能的原因吗?
<details>
<summary>英文:</summary>
I am trying to understand what would be the purpose of a parameter for method: `LockSupport::parkUntil`. Let me give an example of what I mean here:
public static void main(String[] args) {
Object blockedOn = new Object();
System.out.println(blockedOn.hashCode());
Thread parked = new Thread(() -> {
System.out.println("parking the thread");
long howMuch = System.currentTimeMillis() + 5 * 1000;
while (System.currentTimeMillis() < howMuch) {
LockSupport.parkUntil(blockedOn, howMuch);
}
System.out.println("parked the thread");
});
parked.start();
sleepOneSecond();
Object on = LockSupport.getBlocker(parked);
System.out.println(on.hashCode());
}
private static void sleepOneSecond() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
There are actually a few questions, if someone wants to help me understand this. First, is that `while loop`, I don't see any other means to get away from (documentation from `LockSupport::parkUntil`):
> The call spuriously (that is, for no reason) returns.
So, I can only assume that `LockSupport::parkUntil` can fail, for no reason; as such I am forced to wrap this into a loop.
The next question I have is what would be the purpose of that `blockedOn` parameter? I do understand that when waiting long enough, I could do:
Object on = LockSupport.getBlocker(parked);
i.e.: find out the _current_ state of the Object I am blocked on, and as such, somehow react to that. I was thinking I could interrupt the blocked thread (of course it has to support interruptions), but is there any other reason may be?
</details>
# 答案1
**得分**: 3
对于你的第一个问题,这个循环与你在[`Condition`][1]中使用的循环相同,确保在退出“停放”模式时仍满足“状态谓词”。
至于你的第二个问题,`blocker`对象实际上只用于调试目的。[Javadoc中指出][2]
> 每种形式的停放都支持一个阻塞器对象参数。
> 此对象在线程被阻塞时记录,以便监视和诊断工具可以确定线程被阻塞的原因。
请注意,[`unpark`][3]方法不需要`blocker`对象,这表明它不是停放机制的一部分(即与许可证无关)。事实上,您可以使用相同的`blocker`对象停放多个线程。
[1]: https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/util/concurrent/locks/Condition.html
[2]: https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/util/concurrent/locks/LockSupport.html
[3]: https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/util/concurrent/locks/LockSupport.html#unpark(java.lang.Thread)
<details>
<summary>英文:</summary>
For your first question, this loop is the same you'd use for a [`Condition`][1], ensuring that the "state predicate" is still satisfied when you exit the "parked" mode.
As for your second question, the `blocker` object is really only used for debugging purposes. The [Javadoc states][2]
> The three forms of park each also support a blocker object parameter.
> This object is recorded while the thread is blocked to permit
> monitoring and diagnostic tools to identify the reasons that threads
> are blocked.
Note that the [`unpark`][3] method does not need the `blocker` object, suggesting that it isn't used as part of the parking mechanism (ie. unrelated to the permit). In fact, you can park multiple threads with the same `blocker` object.
[1]: https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/util/concurrent/locks/Condition.html
[2]: https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/util/concurrent/locks/LockSupport.html
[3]: https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/util/concurrent/locks/LockSupport.html#unpark(java.lang.Thread)
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论