LockSupport的parkUntil与blocker

huangapple go评论81阅读模式
英文:

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(() -&gt; {
            System.out.println(&quot;parking the thread&quot;);

            long howMuch = System.currentTimeMillis() + 5 * 1000;

            while (System.currentTimeMillis() &lt; howMuch) {
                LockSupport.parkUntil(blockedOn, howMuch);
            }

            System.out.println(&quot;parked the thread&quot;);
        });

        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&#39;t see any other means to get away from (documentation from `LockSupport::parkUntil`):

&gt; 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]

&gt; 每种形式的停放都支持一个阻塞器对象参数。
&gt; 此对象在线程被阻塞时记录,以便监视和诊断工具可以确定线程被阻塞的原因。

请注意,[`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&#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.

As for your second question, the `blocker` object is really only used for debugging purposes. The [Javadoc states][2]

&gt; The three forms of park each also support a blocker object parameter.
&gt; This object is recorded while the thread is blocked to permit
&gt; monitoring and diagnostic tools to identify the reasons that threads
&gt; are blocked.

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.


  [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>



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

发表评论

匿名网友

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

确定