1.简介
LockSupport类可以阻塞当前线程和唤醒指定被阻塞的线程,主要通过park,和unpark(thread)方法来实现阻塞和唤醒线程的操作。
1.当调用unpark(thread)方法,就会将thread线程的许可permit设置为(多次调用unpark方法,不会累加,permit值还是1)。
2.当调用park()方法时,如果当前线程的permit是1,那么将permit的值设置为0,并立即返回。如果当前线程的permit是0,那么当前线程就会阻塞,知道别的线程把当前线程的permit值设置为1,然后park方法再将它设置为0,并返回。
2.源码解析
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); }
}
}
3总结
1.归根到底,LockSupport调用Unsafe中的native代码
public native void unpark(Thread jthread);
public native void park(boolean isAbsolute, long time);
2.关于许可
unpark()放在park之前也可以实现同步,也就是说先提供许可,在操作也是可行的。可以类比到生产者消费者模型理解,先提供许可,然后使用许可。这是他优与wait/notify的地方。
网友评论