源码

JUC

AbstractQueuedSynchronizer

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
public  Abstract class AbstractQueuedSynchronized{

private static final long STATE;
private transient volatile Node head;
private transient volatile Node tail;

//之所以没有设置abstract, 是因为这是一个模板, 有些方法未必所有子类的都有用, 如果abstract修饰
//那子类为了避免成为抽象类就必须重写它不需要的方法
protected boolean tryAcquire(int arg) {
throw new UnsupportedOperationException();
}

protected boolean tryRelease(int arg) {
throw new UnsupportedOperationException();
}

protected int tryAcquireShared(int arg) {
throw new UnsupportedOperationException();
}

final void enqueue(ConditionNode node) {
if (node != null) {
boolean unpark = false;
while(true) {
Node t;
if ((t = this.tail) == null && (t = this.tryInitializeHead()) == null) {
unpark = true;
break;
}
node.setPrevRelaxed(t);
if (this.casTail(t, node)) {
t.next = node;
if (t.status < 0) {
unpark = true;
}
break;
}
}
if (unpark) {
LockSupport.unpark(node.waiter);
}
}

}


}