前言:JUC包,AQS 内部类 Node 源码学习
简介
AQS通过一个双向FIFO同步队列来维护获取对象锁的线程,当获取锁失败的时候,会用当前线程构建一个Node节点,加入到同步队列中去。Node节点中维护了当前线程、status、前驱结点、后继节点、下一个等待节点等。除了CLH队列,还有Condition队列,Condition队列也使用了Node类。
类结构
属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| volatile Node next;
volatile Node prev;
volatile Thread thread;
Node nextWaiter;
static final Node SHARED = new Node();
static final Node EXCLUSIVE = null;
volatile int waitStatus;
static final int CANCELLED = 1;
static final int SIGNAL = -1;
static final int CONDITION = -2;
static final int PROPAGATE = -3;
|
构造函数
三种构造函数
1 2 3 4 5 6 7 8 9 10 11 12 13
| Node() { }
Node(Thread thread, Node mode) { this.nextWaiter = mode; this.thread = thread; }
Node(Thread thread, int waitStatus) { this.waitStatus = waitStatus; this.thread = thread; }
|
isShared
1 2 3 4
| final boolean isShared() { return nextWaiter == SHARED; }
|
predecessor
1 2 3 4 5 6 7 8
| final Node predecessor() throws NullPointerException { Node p = prev; if (p == null) throw new NullPointerException(); else return p; }
|