Understanding java.lang.Thread.State: WAITING (parking)

前端 未结 5 581
隐瞒了意图╮
隐瞒了意图╮ 2020-12-07 11:42

First, a really dumb question, I was just wondering what the waiting \'parking\' means ? Is the thread waiting to be parked or is it just been parked and therefore is in wai

5条回答
  •  旧巷少年郎
    2020-12-07 12:03

    As per the java Thread State Documentation, A thread can go to WAITING state for three reasons:

    1. Object.wait with no timeout
    2. Thread.join with no timeout
    3. LockSupport.park

    When you call a park method on a Thread, it disables the thread for thread scheduling purposes unless the permit is available. You can call unpark method to make available the permit for the given thread, if it was not already available.

    So, when your Thread is in WAITING mode by LockSupport.park, it will show you as WAITING (parking).

    Please make note that, you can call park on current Thread only. This is very helpful mechanism to implement Producer-Consumer Design Pattern.

提交回复
热议问题