码迷,mamicode.com
首页 > 其他好文 > 详细

读LockSupport源码

时间:2015-06-06 15:01:37      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:源码   java   juc   aqs   

LockSupport类简介

LockSupport类是其他类实现锁和同步的基础

 * Basic thread blocking primitives for creating locks and other
 * synchronization classes.

读了源码就会知道,这个类主要利用了Unsafe类中提供的part和unpart两个方法.而LockSupport类暴露出来的两个核心接口也是part和unpart两个.

读源码

//构造方法私有化
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对象上阻塞给定的纳秒时间
// 这个方法和下面的parkUntil方法的不同就是一个是true,一个是false,
// 这两个的具体的细节性区别不怎么清楚,大神赐教啊
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对象上阻塞给定的毫秒时间
public static void parkUntil(Object blocker, long deadline) {
    Thread t = Thread.currentThread();
    setBlocker(t, blocker);
    UNSAFE.park(true, deadline);
    setBlocker(t, null);
}

读LockSupport源码

标签:源码   java   juc   aqs   

原文地址:http://blog.csdn.net/wilsonpeng3/article/details/46387835

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!