Search code examples
javajava.util.concurrent

What is the usage of the parameter of LockSupport.park(Object blocker)?


We can invoke LockSupport.park(Object blocker) to park the current thread.But what is the usage of the parameter "blocker" ?


Solution

  • From the docs:

    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. (Such tools may access blockers using method getBlocker(java.lang.Thread).) The use of these forms rather than the original forms without this parameter is strongly encouraged. The normal argument to supply as a blocker within a lock implementation is this.

    And also:

    blocker - the synchronization object responsible for this thread parking

    You can read more here: LockSupport Docs

    And also there are practical examples well explained here:

    LockSupport examples