LockSupport类能够阻塞当前线程以及唤醒指定被阻塞的线程。主要是经过park()和unpark(thread)方法来实现阻塞和唤醒线程的操做的。java
每一个线程都有一个许可(permit),permit只有两个值1和0,默认是0。多线程
- 当调用unpark(thread)方法,就会将thread线程的许可permit设置成1(注意屡次调用unpark方法,不会累加,permit值仍是1)。
- 当调用park()方法,若是当前线程的permit是1,那么将permit设置为0,并当即返回。若是当前线程的permit是0,那么当前线程就会阻塞,直到别的线程将当前线程的permit设置为1.park方法会将permit再次设置为0,并返回。
注意:由于permit默认是0,因此一开始调用park()方法,线程一定会被阻塞。调用unpark(thread)方法后,会自动唤醒thread线程,即park方法当即返回。dom
import java.util.Queue; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.locks.LockSupport; // 简易的先进先出非重入锁 class FIFOMutex { // private final AtomicBoolean locked = new AtomicBoolean(false); // 记录等待线程队列 private final Queue<Thread> waiters = new ConcurrentLinkedQueue<Thread>(); public void lock() { boolean wasInterrupted = false; Thread current = Thread.currentThread(); waiters.add(current); // 若是当前线程不是等待线程队列第一个,或者locked状态已是true,那么当前线程就要等待 while (waiters.peek() != current || !locked.compareAndSet(false, true)) { System.out.println(Thread.currentThread().getName()+" park start"); LockSupport.park(this); System.out.println(Thread.currentThread().getName()+" park end"); // 等待线程的中断线程标志位为true,就设置wasInterrupted为true if (Thread.interrupted()) wasInterrupted = true; } // 移除第一个元素。当前线程就是第一个元素,由于while判断条件 waiters.remove(); // 若是wasInterrupted为true,当前线程发出中断请求 if (wasInterrupted) current.interrupt(); System.out.println(Thread.currentThread().getName()+" lock end" ); } // 唤醒可能等待的线程 public void unlock() { System.out.println(Thread.currentThread().getName()+" unpark start "); // 将locked设置为false locked.set(false); // 唤醒当前线程队列中第一个元素 LockSupport.unpark(waiters.peek()); System.out.println(Thread.currentThread().getName()+" unpark end "); } } public class LockSupportTest { public static void startThread(String name, final FIFOMutex clock, final CountDownLatch countDownLatch) { new Thread(new Runnable() { @Override public void run() { clock.lock(); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } finally { System.out.println(Thread.currentThread().getName()+" finally"); countDownLatch.countDown(); clock.unlock(); } } }, name).start(); } public static void main(String[] args) { FIFOMutex clock = new FIFOMutex(); CountDownLatch countDownLatch = new CountDownLatch(3); startThread("t111", clock, countDownLatch); startThread("t222", clock, countDownLatch); startThread("t333", clock, countDownLatch); try { countDownLatch.await(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("main end"); } }
从这个例子中能够看出,park方法会阻塞当前线程,unpark(thread)方法,会当即唤醒被阻塞的线程,让它从park方法处继续执行。ide
package java.util.concurrent.locks; import sun.misc.Unsafe; import java.util.concurrent.Semaphore; import java.util.concurrent.ThreadLocalRandom; /** * 提供阻塞线程和唤醒线程的方法。 */ public class LockSupport { // 构造函数是私有的,因此不能在外部实例化 private LockSupport() {} // 用来设置线程t的parkBlocker属性。此对象在线程受阻塞时被记录,以容许监视工具和诊断工具肯定线程受阻塞的缘由。 private static void setBlocker(Thread t, Object arg) { UNSAFE.putObject(t, parkBlockerOffset, arg); } // 唤醒处于阻塞状态下的thread线程 public static void unpark(Thread thread) { // 当线程不为null时调用 if (thread != null) // 经过UNSAFE的unpark唤醒被阻塞的线程 UNSAFE.unpark(thread); } // 阻塞当前线程 public static void park(Object blocker) { Thread t = Thread.currentThread(); // 设置线程t的parkBlocker属性,用于记录线程阻塞状况 setBlocker(t, blocker); // 经过UNSAFE的park方法阻塞线程 UNSAFE.park(false, 0L); setBlocker(t, null); } // 阻塞当前线程nanos纳秒时间,超出时间线程就会被唤醒返回 public static void parkNanos(Object blocker, long nanos) { if (nanos > 0) { Thread t = Thread.currentThread(); setBlocker(t, blocker); UNSAFE.park(false, nanos); setBlocker(t, null); } } // 阻塞当前线程,超过deadline日期线程就会被唤醒返回 public static void parkUntil(Object blocker, long deadline) { Thread t = Thread.currentThread(); setBlocker(t, blocker); UNSAFE.park(true, deadline); setBlocker(t, null); } // 获取线程t的parkBlocker属性 public static Object getBlocker(Thread t) { if (t == null) throw new NullPointerException(); return UNSAFE.getObjectVolatile(t, parkBlockerOffset); } // 阻塞当前线程,不设置parkBlocker属性 public static void park() { UNSAFE.park(false, 0L); } public static void parkNanos(long nanos) { if (nanos > 0) UNSAFE.park(false, nanos); } public static void parkUntil(long deadline) { UNSAFE.park(true, deadline); } static final int nextSecondarySeed() { int r; Thread t = Thread.currentThread(); if ((r = UNSAFE.getInt(t, SECONDARY)) != 0) { r ^= r << 13; // xorshift r ^= r >>> 17; r ^= r << 5; } else if ((r = ThreadLocalRandom.current().nextInt()) == 0) r = 1; // avoid zero UNSAFE.putInt(t, SECONDARY, r); return r; } // Hotspot implementation via intrinsics API private static final Unsafe UNSAFE; private static final long parkBlockerOffset; private static final long SEED; private static final long PROBE; private static final long SECONDARY; static { try { UNSAFE = Unsafe.getUnsafe(); Class<?> tk = Thread.class; parkBlockerOffset = UNSAFE.objectFieldOffset (tk.getDeclaredField("parkBlocker")); SEED = UNSAFE.objectFieldOffset (tk.getDeclaredField("threadLocalRandomSeed")); PROBE = UNSAFE.objectFieldOffset (tk.getDeclaredField("threadLocalRandomProbe")); SECONDARY = UNSAFE.objectFieldOffset (tk.getDeclaredField("threadLocalRandomSecondarySeed")); } catch (Exception ex) { throw new Error(ex); } } }
LockSupport的源码比较简单,主要就是park系列阻塞当前线程的方法,以及unpark唤醒某个线程的方法。函数
注意,park系列的方法就是直接阻塞当前线程的,因此不须要线程变量参数。而unpark方法是唤醒对应线程的,因此必须传递线程变量thread。
在Java多线程详细介绍这篇文章中,咱们介绍了线程一共有六种状态,而park系列方法线程进入两种状态:WAITING等待状态或TIMED_WAITING等待状态。这两种状态都会使线程阻塞在当前位置。
那么怎么唤醒这两种状态的线程呢?工具
对于WAITING等待状态有两种唤醒方式:this
- 调用对应的唤醒方法。这里就是LockSupport的unpark方法。
- 调用该线程变量的interrupt()方法,会唤醒该线程,并抛出InterruptedException异常。
对于TIMED_WAITING等待状态来讲,它比WAITING状态多了一种唤醒方式,就是超过规定时间,那么线程会自动醒来。atom