LockSupport

Park/Unpark provided by LockSupport is designed from a thread perspective and truly decouples synchronization between threads.

Core method

  • Park (), which blocks the current thread until the available permission is obtained, thus blocking the current thread.

The park method can also return “for no reason” at any other time, so it usually has to be called in a loop that rechecks the return condition. Park does not release lock resources held by the current thread.

  • The parkNanos(long) method blocks the current thread until a valid permit is obtained. The maximum wait time is specified by the parameter passed in. Once the maximum time is exceeded, it will also unblock.
  • The parkUntil(long) method, which blocks the current thread until a valid permit is obtained, is set to the deadline specified by the parameter.
  • The park(Object) method is synonymous with the park() method, but most of the arguments it passes are blocking objects.
  • The parkNanos(Object,long) method is synonymous with parkNanos(long), but specifies the blocking Object.
  • The parkUntil(Object,long) method, synonymous with parkUntil(long), specifies the blocking Object.
  • The unpark(Thread) method, which sets the license of the specified Thread to available, wakes up the Thread.

Licensing mechanism

The license for LockSupport usage can be viewed as a binary signal, which has two states: licensed and unlicensed. Each thread has a signal variable. When a thread calls park, it is actually trying to obtain a license. If it succeeds in obtaining a license, it can proceed, otherwise it blocks until it succeeds in obtaining a license. When a thread calls unpark, it releases the license for the thread to acquire. The sequence of execution in park/unpark mode does not affect the wake up and does not cause deadlock.

Park’s response to the interrupt

The park method is interruptible, which means that a thread that calls the park method is blocked can be unblocked and returned immediately if the thread is interrupted. It’s important to note, however, that it doesn’t throw an interrupt exception, so we don’t have to catch InterruptedException.

The difference between thread.sleep () and locksupport.park ()

  • The thread.sleep () and locksupport.park () methods are functionally similar in that they block the execution of the current Thread and do not release any locked resources held by the current Thread.
  • Thread.sleep() can’t wake up from outside, it has to wake up itself; The locksupport.park () method can be awakened by another thread calling the locksupport.unpark () method;
  • The thread.sleep () method declaration throws InterruptedException, so the caller needs to catch or rethrow it; The locksupport.park () method does not need to catch interrupt exceptions;

The difference between object.wait () and locksupport.park ()

  • The object.wait () method needs to be executed in a synchronized block; Locksupport.park () can be executed anywhere;
  • The object.wait () method declares that it throws an interrupt exception that the caller needs to catch or rethrow; Locksupport.park () does not need to catch interrupt exceptions;
  • Object.wait() without a timeout requires another thread to execute notify() to wake up, but not necessarily to continue with the rest of the execution; Locksupport.park () with no timeout requires another thread to execute unpark() to wake it up, and is sure to continue with the rest of the execution;
  • If you wait before () to perform the notify () throws IllegalMonitorStateException exception;
  • If unpark() is executed before park(), the thread will not be blocked, simply skip park() and continue with the rest of the execution;