java.util.concurrent.locks.AbstractQueuedSynchronizerJava
文章发布较早,内容可能过时,阅读注意甄别。
# 成员变量
private volatile int state;
同步状态字段- 设置支持
compareAndSet
private static final Unsafe unsafe = Unsafe.getUnsafe();
private static final long stateOffset;
private static final long headOffset;
private static final long tailOffset;
private static final long waitStatusOffset;
private static final long nextOffset;
static {
try {
stateOffset = unsafe.objectFieldOffset
(AbstractQueuedSynchronizer.class.getDeclaredField("state"));
headOffset = unsafe.objectFieldOffset
(AbstractQueuedSynchronizer.class.getDeclaredField("head"));
tailOffset = unsafe.objectFieldOffset
(AbstractQueuedSynchronizer.class.getDeclaredField("tail"));
waitStatusOffset = unsafe.objectFieldOffset
(Node.class.getDeclaredField("waitStatus"));
nextOffset = unsafe.objectFieldOffset
(Node.class.getDeclaredField("next"));
} catch (Exception ex) { throw new Error(ex); }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 成员函数
- 通过
LockSupport.unpark(s.thread);
解锁,通过LockSupport.park(s.thread);
加锁 tryAcquire
和tryRelease
是获取和释放非公平锁,tryAcquireShare
和tryReleaseShare
是获取和释放公平锁addWaiter(Node mode)
往队列里添加节点,
# 内部类
public class ConditionObject implements Condition, java.io.Serializable
条件对象里有await()
方法做阻塞,signal()
方法做唤醒,主体是对
/** First node of condition queue. */
private transient Node firstWaiter;
/** Last node of condition queue. */
private transient Node lastWaiter;
1
2
3
4
5
2
3
4
5
头节点和尾节点操作
2.static final class Node
阻塞队列里的节点Node,阻塞队列名叫CLH,Node类里的成员变量
static final class Node {
/** Marker to indicate a node is waiting in shared mode */
static final Node SHARED = new Node();
/** Marker to indicate a node is waiting in exclusive mode */
static final Node EXCLUSIVE = null;
/** waitStatus value to indicate thread has cancelled */
static final int CANCELLED = 1;
/** waitStatus value to indicate successor's thread needs unparking */
static final int SIGNAL = -1;
/** waitStatus value to indicate thread is waiting on condition */
static final int CONDITION = -2;
/**
* waitStatus value to indicate the next acquireShared should
* unconditionally propagate
*/
static final int PROPAGATE = -3;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
还有一些Node prev、Node next节点变量,predecessor()方法等,都是对Node的操作。