程序员

三.AbstractQueuedSynchronizer研究

2017-09-15  本文已影响0人  蜗牛1991

一.简介

二.结构

    //阻塞队列头
    private transient volatile Node head;
    //阻塞队列尾
    private transient volatile Node tail;
    //锁的参数,默认为0,不为零则已加锁
    private volatile int state;
  static final class Node {
        //共享锁
        static final Node SHARED = new Node();
        //独占锁
        static final Node EXCLUSIVE = null;
       //Node的四种状态
        static final int CANCELLED =  1;
        static final int SIGNAL    = -1;
        static final int CONDITION = -2;
        static final int PROPAGATE = -3;
      //Node的四种状态
        volatile int waitStatus;
       //Node的前后节点
        volatile Node prev;
        volatile Node next;
        Node nextWaiter;
}
public class ConditionObject implements Condition, java.io.Serializable {
        private transient Node firstWaiter;
        private transient Node lastWaiter;
        private Node addConditionWaiter() {
            Node t = lastWaiter;
            // If lastWaiter is cancelled, clean out.
            if (t != null && t.waitStatus != Node.CONDITION) {
                unlinkCancelledWaiters();
                t = lastWaiter;
            }
            Node node = new Node(Thread.currentThread(), Node.CONDITION);
            if (t == null)
                firstWaiter = node;
            else
                t.nextWaiter = node;
            lastWaiter = node;
            return node;
        }
}
* 供子类实现方法
image.png

三.独占模式

image.png image.png
 private final boolean parkAndCheckInterrupt() {
      //阻塞该线程
      LockSupport.park(this);
     return Thread.interrupted();
 }
1 public final boolean release(int arg) {
2     if (tryRelease(arg)) {
3         Node h = head;
4         if (h != null && h.waitStatus != 0)
5             unparkSuccessor(h);
6         return true;
7     }
8     return false;
9 }

四.共享模式

private void setHeadAndPropagate(Node node, int propagate) {
         Node h = head; // Record old head for check below
         setHead(node);
       if(  proagate > 0 || h == null || h.waitStatus < 0) {
         Node s = node.next;
         if (s == null || s.isShared())
            doReleaseShared();
     }
 }

 private void doReleaseShared() {
        for (;;) {
            Node h = head;
            if (h != null && h != tail) {
                int ws = h.waitStatus;
                if (ws == Node.SIGNAL) {
                    if (!compareAndSetWaitStatus(h, Node.SIGNAL, 0))
                        continue;            // loop to recheck cases
                    unparkSuccessor(h);
                }
                else if (ws == 0 &&
                         !compareAndSetWaitStatus(h, 0, Node.PROPAGATE))
                    continue;                // loop on failed CAS
            }
            if (h == head)                   // loop if head changed
                break;
        }
    }

五.Condition通知/等待队列

image.png image.png
   final boolean transferForSignal(Node node) {
        if (!compareAndSetWaitStatus(node, Node.CONDITION, 0))
            return false;
        Node p = enq(node);
        int ws = p.waitStatus;
        if (ws > 0 || !compareAndSetWaitStatus(p, ws, Node.SIGNAL))
            LockSupport.unpark(node.thread);
        return true;
    }

参考:再谈AbstractQueuedSynchronizer2:共享模式与基于Condition的等待/通知机制实现

上一篇下一篇

猜你喜欢

热点阅读