LockSupport's park and unpark

 LockSupport is a relatively low-level class in the JDK, used to create basic thread blocking primitives for locks and other synchronization tool classes.

     The core AQS of the Java lock and synchronizer framework: AbstractQueuedSynchronizer, which blocks and wakes up threads by calling LockSupport.park() and LockSupport.unpark(). LockSupport is very similar to a binary semaphore (only 1 license is available), if the license has not been occupied, the current thread obtains the permission and continues to execute; if the permission is already occupied, the current thread blocks, waiting to obtain the permission.

     The functions of park() and unpark() in LockSupport are to block and unblock threads respectively, and park() and unpark() will not encounter the problem of "deadlock that may be caused by Thread.suspend and Thread.resume". Because park() and unpark() have permissions; the race between the thread calling park() and another thread trying to unpark() it will remain alive.

List of LockSupport functions

// Returns the blocker object provided to the most recent unblocked park method call, or null if the call is not blocking.
static Object getBlocker(Thread t)
// For thread scheduling, disable the current thread unless permission is available.
static void park()
// For thread scheduling purposes, disable the current thread until a permit is available.
static void park(Object blocker)
// Disables the current thread for thread scheduling, waiting at most the specified wait time unless permission is available.
static void parkNanos(long nanos)
// For thread scheduling purposes, disable the current thread until permission is available and wait up to the specified wait time.
static void parkNanos(Object blocker, long nanos)
// For thread scheduling purposes, disable the current thread until the specified time limit unless permission is available.
static void parkUntil(long deadline)
// For thread scheduling, disable the current thread until the specified deadline, unless permission is available.
static void parkUntil(Object blocker, long deadline)
// If the permission for the given thread is not available yet, make it available.
static void unpark(Thread thread)

Description: LockSupport implements blocking and unblocking by calling the interface in the Unsafe function.

The difference between park and wait

Before calling Wait on the object, the current thread must first obtain the monitor of the object (Synchronized), and after being woken up, it needs to obtain the monitor again to continue execution.

And LockSupport does not need to get the object's monitor. The LockSupport mechanism is that each unpark gives the thread 1 "permit" - at most 1, while park is the opposite. If the current thread has permission, the park method will consume 1 and return, otherwise the thread will be blocked until the thread is reacquired Permitted, calling park/unpark methods before the thread starts has no effect.

Because their implementation mechanisms are different, there is no intersection between them, which means that the thread blocked by LockSupport cannot be woken up by notify/notifyAll.

Summarize the park/unpark of LockSupport and the wait/notify of Object:

  • different objects;
  • Unlike Object's wait/notify, LockSupport's park/unpark does not need to obtain the object's monitor;
  • The mechanisms implemented are different, so there is no intersection between the two.

Although the usage of the two is different, there is one thing that  LockSupport the park and the wait of the Object can also respond to interrupts.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325330768&siteId=291194637