ReentrantLockd的unlock方法实现源码解析

读ReentrantLock的unlock()源码:
//这个方法:委托到sync的release实现。
   public void unlock() {
        sync.release(1);
    }


public final boolean release(int arg) {
        //释放锁成功
        if (tryRelease(arg)) {
            Node h = head;
	   
            if (h != null && h.waitStatus != 0)
	      
                unparkSuccessor(h);
            return true;
        }
        return false;
    }

 protected final boolean tryRelease(int releases) {
            
            int c = getState() - releases;
            if (Thread.currentThread() != getExclusiveOwnerThread())
                throw new IllegalMonitorStateException();
            boolean free = false;
	    //如果c==0,即getState为1,如果c!=0,说明锁有重入。
            if (c == 0) {
                free = true;
                setExclusiveOwnerThread(null);
            }
            setState(c);
            return free;
        }

   
 //唤醒第二个node
    private void unparkSuccessor(Node node) {
        
        int ws = node.waitStatus;
        if (ws < 0)
	    //设置第一个节点状态为0
            compareAndSetWaitStatus(node, ws, 0);

       //获取第二个节点
        Node s = node.next;
        if (s == null || s.waitStatus > 0) {
            s = null;
	    //从队尾开始循环直到拿到离note最近的状态<0的节点
            for (Node t = tail; t != null && t != node; t = t.prev)
                if (t.waitStatus <= 0)
                    s = t;
        }
	//解锁唤醒一个等待的线程
        if (s != null)
            LockSupport.unpark(s.thread);
    }

相关推荐