LockSupport类简介
LockSupport类是其他类实现锁和同步的基础
* Basic thread blocking primitives for creating locks and other
* synchronization classes.
读了源码就会知道,这个类主要利用了Unsafe类中提供的part和unpart两个方法.而LockSupport类暴露出来的两个核心接口也是part和unpart两个.
如果需要阅读Unsafe类源码,参考我的另一篇博文:读Unsafe类源码
读源码
//构造方法私有化
private LockSupport() {} // Cannot be instantiated.
// 引用Unsafe类
private static final sun.misc.Unsafe UNSAFE;
//Thread类中 parkBlocker 字段的偏移量
private static final long parkBlockerOffset;
//Thread 类中 threadLocalRandomSeed 字段的偏移量
private static final long SEED;
//Thread 类中 threadLocalRandomProbe 字段的偏移量
private static final long PROBE;
//Thread 类中 threadLocalRandomSecondarySeed 字段的偏移量
private static final long SECONDARY;
//初始化上面4个字段的值
static {
try {
UNSAFE = sun.misc.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); }
}
//把 Thread 实例 t 的 parkBlocker 字段的值设置为 arg
private static void setBlocker(Thread t, Object arg) {
// Even though volatile, hotspot doesn't need a write barrier here.
UNSAFE.putObject(t, parkBlockerOffset, arg);
}
//获取对象 t 中 parkBlocker 字段的值
public static Object getBlocker(Thread t) {
if (t == null)
throw new NullPointerException();
return UNSAFE.getObjectVolatile(t, parkBlockerOffset);
}
// 取消阻塞线程,如果线程已经处于非阻塞状态,那么下次调用park时不会阻塞线程
public static void unpark(Thread thread) {
if (thread != null)
UNSAFE.unpark(thread);
}
// 使当前调用线程在给定对象上阻塞(不能保证一定阻塞,
// 因为如果之前在非阻塞状态调用了unpar方法的话,此次调用park方法就不会阻塞线程)
public static void park(Object blocker) {
Thread t = Thread.currentThread();
setBlocker(t, blocker);
UNSAFE.park(false, 0L);
setBlocker(t, null);
}
// 阻塞线程
public static void park() {
UNSAFE.park(false, 0L);
}
// 使当前线程在blocker对象上阻塞给定的纳秒时间
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);
}
}
// 使当前线程在blocker对象上阻塞到给定的时间点
// 这个时间点是从Epoch time(1970-01-01 00:00:00 UTC)开始算起的某个具体的时间点。
public static void parkUntil(Object blocker, long deadline) {
Thread t = Thread.currentThread();
setBlocker(t, blocker);
UNSAFE.park(true, deadline);
setBlocker(t, null);
}
欢迎关注订阅号: