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

手写一个简易的ReentrantLock

时间:2020-10-22 23:07:16      阅读:22      评论:0      收藏:0      [点我收藏+]

标签:线程等待   int   自旋   read   boolean   and   bre   off   tran   

public class HqaLock {
    private static final Unsafe unsafe = reflectGetUnsafe();
    private static final long ownerOffset;

    /**
     * 锁是否被持有(1)是,(0)否
     */
    AtomicInteger state = new AtomicInteger(0);
    /**
     * 当前持有锁的线程
     */
    volatile Thread owner = new Thread();
    /**
     * 线程等待池
     */
    private Queue<Thread> waitQueue = new LinkedBlockingQueue();
    /**
     * 重入次数
     */
    AtomicInteger reentrantCount = new AtomicInteger(0);

    private static Unsafe reflectGetUnsafe() {
        try {
            Field field = Unsafe.class.getDeclaredField("theUnsafe");
            field.setAccessible(true);
            return (Unsafe) field.get(null);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    static {
        try {
            ownerOffset = unsafe.objectFieldOffset
                    (HqaLock.class.getDeclaredField("owner"));
        } catch (Exception ex) { throw new Error(ex); }
    }

    public void lock(){
        //未被持有则直接获取锁
        if (state.get() == 0){
            //自旋cas改变锁状态
            for(;;){
                boolean b = state.compareAndSet(0,1);
                if (b == true){
                    unsafe.compareAndSwapObject(this,ownerOffset,owner,Thread.currentThread());
                    owner = Thread.currentThread();
                    reentrantCount.set(1);
                    break;
                }
            }

        }else {//已经被持有
            //持有锁的线程是是自己
            if (unsafe.getObject(owner,ownerOffset) == Thread.currentThread()){
                reentrantCount.getAndAdd(1);
            }else {
                waitQueue.add(Thread.currentThread());
                //持有锁的线程不是自己
                while (true){
                    //阻塞休眠
                    LockSupport.park();
                    //如果锁被持有者释放了
                    if (state.get() == 0){
                        owner = Thread.currentThread();
                        state.set(1);
                        break;
                    }
                }
            }
        }
    }

    public void unlock() throws Exception {

        if (unsafe.getObject(this,ownerOffset) != Thread.currentThread()){
            throw new Exception("该线程未持有锁");
        }
        //重入次数减1
        reentrantCount.getAndDecrement();
        //直到重入锁全部解开的才能真正的解锁
        if (reentrantCount.get() == 0){
            state.set(0);
            Thread head = waitQueue.peek();
            LockSupport.unpark(head);
        }

    }
}

  

手写一个简易的ReentrantLock

标签:线程等待   int   自旋   read   boolean   and   bre   off   tran   

原文地址:https://www.cnblogs.com/huqingan/p/13860264.html

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