This is the 22nd day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

LockSupport in the Rt.jar package in the JDK is a utility class whose main purpose is to suspend and wake up threads and which is the basis for creating locks and other synchronization classes.

The LockSupport class is associated with a license for each thread that uses it, and threads that invoke methods of the LockSupport class by default do not hold licenses. LockSupport is implemented using the Unsafe class. Here are some functions in LockSupport.

1. The void park () method

If the thread calling park has a license associated with LockSupport, then the call to locksupport.park () will return immediately, otherwise the calling thread will be blocked and suspended.

public static void main(String[] args) {
    System.out.println("begin park");
    LockSupport.park();
    System.out.println("end park");
}
Copy the code

The blocked thread that called the park method returns when another thread calls the unpark() method and takes the current thread as an argument. The blocking thread also returns if another thread calls interrupt(), sets the interrupt flag, or if the thread is falsely awakened. So when calling the Park method, it is best to use circular condition judgment. A thread that is blocked by calling the park() method is interrupted by another thread and returns without throwing InterruptedException.

  1. Void unpark(Thread Thread) method

When a thread calls unpark, let the thread thread hold the license associated with the LockSupport class if the parameter thread thread does not hold it. If the thread was previously suspended by calling park(), it will be woken up after calling unpark. If thread has not called park before, the unpark method is followed by the park method, which returns immediately.

public static void main(String[] args) throws InterruptedException{
    Thread thread = new Thread(new Runnable() {
        @Override
        public void run(a) {
            System.out.println("child thread begin park!");
            LockSupport.park();
            System.out.println("child thread unpark!"); }}); Thread.sleep(1000);
    System.out.println("main thread begin unpark!");
    LockSupport.unpark(thread);
}
Copy the code

The park method does not tell you why it returned, so the caller needs to check again if the condition is met based on the reason the park method was called before. If not, the park method needs to be called again. For example, we modify the above example code to remove locksupport.unpark (thread) based on the return of the interrupted thread after calling the park method; Then add thread.interrupt().

public static void main(String[] args) throws InterruptedException{
    Thread thread = new Thread(new Runnable() {
        @Override
        public void run(a) {
            System.out.println("child thread begin park!");
            while(! Thread.currentThread().isInterrupted()) { LockSupport.park(); } System.out.println("child thread unpark!"); }}); Thread.sleep(1000);
    System.out.println("main thread begin unpark!");
    thread.interrupt();
}
Copy the code

In the code above, the child thread is terminated only if it is interrupted. If the child thread is not interrupted, the child thread does not terminate even if the unpark method is called.

  1. Void parkNanos(Long Nanos) method

The park method is similar to the park method, except that it automatically returns when the call to park is suspended for namOS time.

  1. park (Object blocker)
public static void park(Object blocker) {
    Thread t = Thread.currentThread();
    setBlocker(t, blocker);
    UNSAFE.park(false, 0L);
    setBlocker(t, null);
}
Copy the code

The Park method also supports a method with a Blocker argument, which is logged to the thread when it is blocked and suspended by calling the Park method without a license.