LockSupport的parkUntil与blocker

huangapple 未分类评论42阅读模式
英文:

LockSupport parkUntil with blocker

问题

  1. 我正在尝试理解方法 `LockSupport::parkUntil` 的参数用途是什么让我通过一个示例来说明我的意思
  2. public static void main(String[] args) {
  3. Object blockedOn = new Object();
  4. System.out.println(blockedOn.hashCode());
  5. Thread parked = new Thread(() -> {
  6. System.out.println("parking the thread");
  7. long howMuch = System.currentTimeMillis() + 5 * 1000;
  8. while (System.currentTimeMillis() < howMuch) {
  9. LockSupport.parkUntil(blockedOn, howMuch);
  10. }
  11. System.out.println("parked the thread");
  12. });
  13. parked.start();
  14. sleepOneSecond();
  15. Object on = LockSupport.getBlocker(parked);
  16. System.out.println(on.hashCode());
  17. }
  18. private static void sleepOneSecond() {
  19. try {
  20. Thread.sleep(1000);
  21. } catch (InterruptedException e) {
  22. throw new RuntimeException(e);
  23. }
  24. }

关于这段代码,实际上有一些问题,如果有人想帮助我理解的话。首先,就是那个 while 循环,我没有看到任何其他方式可以摆脱它(来自 LockSupport::parkUntil 的文档):

调用可能(也就是毫无理由地)会返回。

因此,我只能假设 LockSupport::parkUntil 可能会无故失败,因此我被迫将其放入循环中。

我接下来的问题是,blockedOn 参数的目的是什么?我理解,当等待足够长的时间后,我可以这样做:

  1. Object on = LockSupport.getBlocker(parked);

也就是找出我被阻塞在哪个对象上的 当前 状态,然后根据这个状态做出相应的反应。我在考虑是否可以中断被阻塞的线程(当然它必须支持中断),但还有其他可能的原因吗?

  1. <details>
  2. <summary>英文:</summary>
  3. 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:
  4. public static void main(String[] args) {
  5. Object blockedOn = new Object();
  6. System.out.println(blockedOn.hashCode());
  7. Thread parked = new Thread(() -&gt; {
  8. System.out.println(&quot;parking the thread&quot;);
  9. long howMuch = System.currentTimeMillis() + 5 * 1000;
  10. while (System.currentTimeMillis() &lt; howMuch) {
  11. LockSupport.parkUntil(blockedOn, howMuch);
  12. }
  13. System.out.println(&quot;parked the thread&quot;);
  14. });
  15. parked.start();
  16. sleepOneSecond();
  17. Object on = LockSupport.getBlocker(parked);
  18. System.out.println(on.hashCode());
  19. }
  20. private static void sleepOneSecond() {
  21. try {
  22. Thread.sleep(1000);
  23. } catch (InterruptedException e) {
  24. throw new RuntimeException(e);
  25. }
  26. }
  27. There are actually a few questions, if someone wants to help me understand this. First, is that `while loop`, I don&#39;t see any other means to get away from (documentation from `LockSupport::parkUntil`):
  28. &gt; The call spuriously (that is, for no reason) returns.
  29. So, I can only assume that `LockSupport::parkUntil` can fail, for no reason; as such I am forced to wrap this into a loop.
  30. 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:
  31. Object on = LockSupport.getBlocker(parked);
  32. 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?
  33. </details>
  34. # 答案1
  35. **得分**: 3
  36. 对于你的第一个问题,这个循环与你在[`Condition`][1]中使用的循环相同,确保在退出“停放”模式时仍满足“状态谓词”。
  37. 至于你的第二个问题,`blocker`对象实际上只用于调试目的。[Javadoc中指出][2]
  38. &gt; 每种形式的停放都支持一个阻塞器对象参数。
  39. &gt; 此对象在线程被阻塞时记录,以便监视和诊断工具可以确定线程被阻塞的原因。
  40. 请注意,[`unpark`][3]方法不需要`blocker`对象,这表明它不是停放机制的一部分(即与许可证无关)。事实上,您可以使用相同的`blocker`对象停放多个线程。
  41. [1]: https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/util/concurrent/locks/Condition.html
  42. [2]: https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/util/concurrent/locks/LockSupport.html
  43. [3]: https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/util/concurrent/locks/LockSupport.html#unpark(java.lang.Thread)
  44. <details>
  45. <summary>英文:</summary>
  46. For your first question, this loop is the same you&#39;d use for a [`Condition`][1], ensuring that the &quot;state predicate&quot; is still satisfied when you exit the &quot;parked&quot; mode.
  47. As for your second question, the `blocker` object is really only used for debugging purposes. The [Javadoc states][2]
  48. &gt; The three forms of park each also support a blocker object parameter.
  49. &gt; This object is recorded while the thread is blocked to permit
  50. &gt; monitoring and diagnostic tools to identify the reasons that threads
  51. &gt; are blocked.
  52. Note that the [`unpark`][3] method does not need the `blocker` object, suggesting that it isn&#39;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.
  53. [1]: https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/util/concurrent/locks/Condition.html
  54. [2]: https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/util/concurrent/locks/LockSupport.html
  55. [3]: https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/util/concurrent/locks/LockSupport.html#unpark(java.lang.Thread)
  56. </details>

huangapple
  • 本文由 发表于 2020年4月4日 00:13:53
  • 转载请务必保留本文链接:https://java.coder-hub.com/61016072.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定