Home  >  Article  >  Java  >  Detailed explanation of thread blocking usage of LockSupport class in Java multi-thread programming

Detailed explanation of thread blocking usage of LockSupport class in Java multi-thread programming

高洛峰
高洛峰Original
2017-01-05 15:56:401471browse

LockSupport is the basic thread blocking primitive used to create locks and other synchronization classes.
The functions of park() and unpark() in LockSupport are to block threads and unblock threads respectively, and park() and unpark() will not encounter deadlocks that may be caused by "Thread.suspend and Thread.resume" "question.
Because park() and unpark() have permissions; the race between the thread calling park() and another thread trying to unpark() it will remain active.

Basic usage
LockSupport is very similar to a binary semaphore (only 1 license is available for use). If this license has not been occupied, the current thread obtains the license and continues execution; if the license has been Occupied, the current thread is blocked, waiting to obtain permission.

public static void main(String[] args)
{
   LockSupport.park();
   System.out.println("block.");
}

When you run this code, you can find that the main thread is always blocked. Because the license is occupied by default, the license cannot be obtained when calling park(), so it enters the blocking state.

The following code: release the permission first, then obtain the permission, and the main thread can terminate normally. The acquisition and release of LockSupport permissions are generally corresponding. If you unpark multiple times, there will be no problem with just one park. The result is that the permission is available.

public static void main(String[] args)
{
   Thread thread = Thread.currentThread();
   LockSupport.unpark(thread);//释放许可
   LockSupport.park();// 获取许可
   System.out.println("b");
}

LockSupport is not reentrant. If a thread calls LockSupport .park() twice in a row, the thread will definitely be blocked forever.

public static void main(String[] args) throws Exception
{
 Thread thread = Thread.currentThread();
  
 LockSupport.unpark(thread);
  
 System.out.println("a");
 LockSupport.park();
 System.out.println("b");
 LockSupport.park();
 System.out.println("c");
}

This code prints out a and b, but not c, because when park is called for the second time, the thread cannot obtain the permission and a deadlock occurs.

Let’s take a look at the responsiveness of LockSupport to interrupts

public static void t2() throws Exception
{
 Thread t = new Thread(new Runnable()
 {
  private int count = 0;
 
  @Override
  public void run()
  {
   long start = System.currentTimeMillis();
   long end = 0;
 
   while ((end - start) <= 1000)
   {
    count++;
    end = System.currentTimeMillis();
   }
 
   System.out.println("after 1 second.count=" + count);
 
  //等待或许许可
   LockSupport.park();
   System.out.println("thread over." + Thread.currentThread().isInterrupted());
 
  }
 });
 
 t.start();
 
 Thread.sleep(2000);
 
 // 中断线程
 t.interrupt();
 
  
 System.out.println("main over");
}

The final thread will print out thread over.true. This shows that if the thread is blocked by calling park, it can respond to the interrupt request (the interrupt status is set to true), but it will not throw InterruptedException.

LockSupport function list

// 返回提供给最近一次尚未解除阻塞的 park 方法调用的 blocker 对象,如果该调用不受阻塞,则返回 null。
static Object getBlocker(Thread t)
// 为了线程调度,禁用当前线程,除非许可可用。
static void park()
// 为了线程调度,在许可可用之前禁用当前线程。
static void park(Object blocker)
// 为了线程调度禁用当前线程,最多等待指定的等待时间,除非许可可用。
static void parkNanos(long nanos)
// 为了线程调度,在许可可用前禁用当前线程,并最多等待指定的等待时间。
static void parkNanos(Object blocker, long nanos)
// 为了线程调度,在指定的时限前禁用当前线程,除非许可可用。
static void parkUntil(long deadline)
// 为了线程调度,在指定的时限前禁用当前线程,除非许可可用。
static void parkUntil(Object blocker, long deadline)
// 如果给定线程的许可尚不可用,则使其可用。
static void unpark(Thread thread)

##LockSupport example
Compare the following "Example 1" and "Example 2" to change Clearly understand the usage of LockSupport.
Example 1

public class WaitTest1 {
 
  public static void main(String[] args) {
 
    ThreadA ta = new ThreadA("ta");
 
    synchronized(ta) { // 通过synchronized(ta)获取“对象ta的同步锁”
      try {
        System.out.println(Thread.currentThread().getName()+" start ta");
        ta.start();
 
        System.out.println(Thread.currentThread().getName()+" block");
        // 主线程等待
        ta.wait();
 
        System.out.println(Thread.currentThread().getName()+" continue");
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
  }
 
  static class ThreadA extends Thread{
 
    public ThreadA(String name) {
      super(name);
    }
 
    public void run() {
      synchronized (this) { // 通过synchronized(this)获取“当前对象的同步锁”
        System.out.println(Thread.currentThread().getName()+" wakup others");
        notify();  // 唤醒“当前对象上的等待线程”
      }
    }
  }
}

Example 2

import java.util.concurrent.locks.LockSupport;
 
public class LockSupportTest1 {
 
  private static Thread mainThread;
 
  public static void main(String[] args) {
 
    ThreadA ta = new ThreadA("ta");
    // 获取主线程
    mainThread = Thread.currentThread();
 
    System.out.println(Thread.currentThread().getName()+" start ta");
    ta.start();
 
    System.out.println(Thread.currentThread().getName()+" block");
    // 主线程阻塞
    LockSupport.park(mainThread);
 
    System.out.println(Thread.currentThread().getName()+" continue");
  }
 
  static class ThreadA extends Thread{
 
    public ThreadA(String name) {
      super(name);
    }
 
    public void run() {
      System.out.println(Thread.currentThread().getName()+" wakup others");
      // 唤醒“主线程”
      LockSupport.unpark(mainThread);
    }
  }
}

Run result:

main start ta
main block
ta wakup others
main continue

Explanation: The difference between park and wait. Before wait blocks the thread, it must obtain the synchronization lock through synchronized.


For more detailed explanations on the thread blocking usage of the LockSupport class in Java multi-threaded programming, please pay attention to the PHP Chinese website for related articles!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn