-
Notifications
You must be signed in to change notification settings - Fork 1
/
AbstractQueuedSynchronizer.drawio
29 lines (29 loc) · 12.2 KB
/
AbstractQueuedSynchronizer.drawio
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
<mxfile host="Electron" modified="2024-04-13T15:49:54.591Z" agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) draw.io/21.7.5 Chrome/114.0.5735.289 Electron/25.8.1 Safari/537.36" etag="2ElhVW6iYNkns6tVej8n" version="21.7.5" type="device">
<diagram id="C5RBs43oDa-KdzZeNtuy" name="AQS">
<mxGraphModel dx="2776" dy="855" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="827" pageHeight="1169" math="0" shadow="0">
<root>
<mxCell id="WIyWlLk6GJQsqaUBKTNV-0" />
<mxCell id="WIyWlLk6GJQsqaUBKTNV-1" parent="WIyWlLk6GJQsqaUBKTNV-0" />
<mxCell id="u982tnG7eZZK8EAwPbnZ-12" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0.5;exitY=0;exitDx=0;exitDy=0;endArrow=block;endFill=0;" edge="1" parent="WIyWlLk6GJQsqaUBKTNV-1" source="u982tnG7eZZK8EAwPbnZ-13" target="u982tnG7eZZK8EAwPbnZ-11">
<mxGeometry relative="1" as="geometry">
<mxPoint x="-260" y="260" as="sourcePoint" />
</mxGeometry>
</mxCell>
<mxCell id="u982tnG7eZZK8EAwPbnZ-11" value="<p style="margin: 4px 0px 0px; text-align: center; font-size: 12px;"><b style="font-size: 12px;">AbstractOwnableSynchronizer (A)</b><br style="font-size: 12px;"></p><hr style="font-size: 12px;"><p style="border-color: var(--border-color); margin: 0px 0px 0px 8px; font-size: 12px;"><font style="border-color: var(--border-color); font-size: 12px;" color="#007fff">用于<b style="font-size: 12px;">支持独占锁实现</b>的同步器</font></p><p style="border-color: var(--border-color); margin: 0px 0px 0px 8px; font-size: 12px;"><font style="border-color: var(--border-color); font-size: 12px;" color="#007fff"><br style="font-size: 12px;"></font></p><p style="border-color: var(--border-color); margin: 0px 0px 0px 8px; font-size: 12px;"><font style="border-color: var(--border-color); font-size: 12px;" color="#007fff">//记录独占锁的线程(对于排他锁的实现会使用到这个成员变量,ReentrantLock)</font></p><p style="border-color: var(--border-color); margin: 0px 0px 0px 8px; font-size: 12px;">private transient Thread <b style="font-size: 12px;">exclusiveOwnerThread</b>;</p><hr style="font-size: 12px;"><p style="margin: 0px 0px 0px 4px;"><font color="#007fff">//记录独占锁的线程,线程成功抢占锁时记录</font></p><p style="margin: 0px 0px 0px 4px;">protected final void setExclusiveOwnerThread(Thread thread)<br></p><p style="margin: 0px 0px 0px 4px;">protected final Thread getExclusiveOwnerThread()<br></p>" style="verticalAlign=top;align=left;overflow=fill;fontSize=12;fontFamily=Helvetica;html=1;whiteSpace=wrap;" vertex="1" parent="WIyWlLk6GJQsqaUBKTNV-1">
<mxGeometry x="-520" y="40" width="480" height="180" as="geometry" />
</mxCell>
<mxCell id="u982tnG7eZZK8EAwPbnZ-15" style="rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=-0.002;exitY=0.43;exitDx=0;exitDy=0;endArrow=open;endFill=0;exitPerimeter=0;entryX=1;entryY=0.5;entryDx=0;entryDy=0;" edge="1" parent="WIyWlLk6GJQsqaUBKTNV-1" source="u982tnG7eZZK8EAwPbnZ-13" target="u982tnG7eZZK8EAwPbnZ-16">
<mxGeometry relative="1" as="geometry">
<mxPoint x="-560" y="426.752411575563" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="u982tnG7eZZK8EAwPbnZ-13" value="<p style="margin:0px;margin-top:4px;text-align:center;"><b>AbstractQueuedSynchronizer (A)</b><br></p><hr size="1"><p style="margin:0px;margin-left:4px;"><font color="#007fff">AQS核心就是一个<b>双向队列(CLH)</b></font></p><p style="margin:0px;margin-left:4px;"><font color="#007fff"><br></font></p><p style="margin:0px;margin-left:4px;"><font color="#007fff">// Node status bits, also used as argument and return values</font></p><p style="margin:0px;margin-left:4px;">static final int WAITING&nbsp; &nbsp;= 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // must be 1</p><p style="margin:0px;margin-left:4px;">static final int CANCELLED = 0x80000000; // must be negative</p><p style="margin:0px;margin-left:4px;"></p><p style="margin:0px;margin-left:4px;">static final int COND&nbsp; &nbsp; &nbsp; = 2;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // in a condition wait</p><p style="margin:0px;margin-left:4px;"><font color="#007fff"><br></font></p><p style="margin:0px;margin-left:4px;"><font color="#007fff">//自旋超时时间</font></p><p style="margin:0px;margin-left:4px;">static final long <b>spinForTimeoutThreshold</b> = 1000L;</p><p style="margin:0px;margin-left:4px;"><font color="#007fff">//等待队列的头节点(等待队列中存放等待获取锁并被唤醒的线程)</font></p><p style="margin:0px;margin-left:4px;">private transient volatile <b>Node</b> <b>head</b>;</p><p style="margin:0px;margin-left:4px;"><font color="#007fff">//等待队列的尾节点</font></p><p style="margin:0px;margin-left:4px;">private transient volatile <b>Node</b> <b>tail</b>;</p><p style="margin:0px;margin-left:4px;"><font color="#007fff">//锁状态,0:锁未被占用,1:锁被占用,&gt;1:锁被重入占用(重入次数)</font></p><p style="margin:0px;margin-left:4px;">private volatile int <b>state</b>;</p><p style="margin:0px;margin-left:4px;"><br></p><p style="margin:0px;margin-left:4px;"><font color="#007fff">//CAS操作入口 及 各个属性的偏移量</font></p><p style="margin:0px;margin-left:4px;">private static final Unsafe <b>unsafe</b> = Unsafe.getUnsafe();</p><p style="margin:0px;margin-left:4px;">private static final long stateOffset;</p><p style="margin:0px;margin-left:4px;">private static final long headOffset;</p><p style="margin:0px;margin-left:4px;">private static final long tailOffset;</p><p style="margin:0px;margin-left:4px;">private static final long waitStatusOffset; //Node.waitStatus的偏移量</p><p style="margin:0px;margin-left:4px;">private static final long nextOffset;</p><hr size="1"><p style="margin:0px;margin-left:4px;"><br></p>" style="verticalAlign=top;align=left;overflow=fill;fontSize=12;fontFamily=Helvetica;html=1;whiteSpace=wrap;fillColor=#fff2cc;strokeColor=#d6b656;" vertex="1" parent="WIyWlLk6GJQsqaUBKTNV-1">
<mxGeometry x="-520" y="260" width="480" height="420" as="geometry" />
</mxCell>
<mxCell id="u982tnG7eZZK8EAwPbnZ-16" value="<p style="margin: 4px 0px 0px; text-align: center; font-size: 12px;"><b>Node</b></p><hr style="font-size: 12px;"><p style="border-color: var(--border-color); margin: 0px 0px 0px 8px; font-size: 12px;"><font style="border-color: var(--border-color); font-size: 12px;" color="#007fff">CLH队列的节点,存储等待中的线程以及线程的状态</font></p><p style="border-color: var(--border-color); margin: 0px 0px 0px 8px; font-size: 12px;"><font style="border-color: var(--border-color); font-size: 12px;" color="#007fff"><br></font></p><p style="border-color: var(--border-color); margin: 0px 0px 0px 8px;"><font color="#007fff">//线程已经取消获取锁</font></p><p style="border-color: var(--border-color); margin: 0px 0px 0px 8px;">static final int CANCELLED =&nbsp; 1;</p><p style="border-color: var(--border-color); margin: 0px 0px 0px 8px;"><font color="#007fff">//继任者线程需要 unparking</font></p><p style="border-color: var(--border-color); margin: 0px 0px 0px 8px;">static final int SIGNAL&nbsp; &nbsp; = -1;</p><p style="border-color: var(--border-color); margin: 0px 0px 0px 8px;"><font color="#007fff">//线程正在等待条件</font></p><p style="border-color: var(--border-color); margin: 0px 0px 0px 8px;">static final int CONDITION = -2;</p><p style="border-color: var(--border-color); margin: 0px 0px 0px 8px;"><font color="#007fff">//下个 acquireShared 需要无条件传播</font></p><p style="border-color: var(--border-color); margin: 0px 0px 0px 8px;"><font style="border-color: var(--border-color);" color="#007fff"></font></p><p style="border-color: var(--border-color); margin: 0px 0px 0px 8px;">static final int PROPAGATE = -3;</p><p style="border-color: var(--border-color); margin: 0px 0px 0px 8px;"><br></p><p style="border-color: var(--border-color); margin: 0px 0px 0px 8px; font-size: 12px;"><font style="border-color: var(--border-color); font-size: 12px;"></font></p><p style="border-color: var(--border-color); margin: 0px 0px 0px 8px; font-size: 12px;"><font style="border-color: var(--border-color); font-size: 12px;" color="#007fff">//节点线程的等待状态</font></p><p style="border-color: var(--border-color); margin: 0px 0px 0px 8px; font-size: 12px;"><font style="border-color: var(--border-color); font-size: 12px;" color="#007fff"></font></p><p style="border-color: var(--border-color); margin: 0px 0px 0px 8px; font-size: 12px;"><font style="border-color: var(--border-color); font-size: 12px;">volatile int <b>waitStatus</b>;</font></p><p style="border-color: var(--border-color); margin: 0px 0px 0px 8px; font-size: 12px;"><font style="border-color: var(--border-color); font-size: 12px;" color="#007fff">//prev next 说明是一个双向队列<br style="font-size: 12px;"></font></p><p style="border-color: var(--border-color); margin: 0px 0px 0px 8px;">volatile Node <b>prev</b>;</p><p style="border-color: var(--border-color); margin: 0px 0px 0px 8px;">volatile Node <b>next</b>;</p><p style="border-color: var(--border-color); margin: 0px 0px 0px 8px;"><font color="#007fff">//等待中的线程</font></p><p style="border-color: var(--border-color); margin: 0px 0px 0px 8px;">volatile Thread <b>thread</b>;</p><p style="border-color: var(--border-color); margin: 0px 0px 0px 8px;">Node <b>nextWaiter</b>;<br></p><hr style="font-size: 12px;"><p style="margin: 0px 0px 0px 4px;"><br></p>" style="verticalAlign=top;align=left;overflow=fill;fontSize=12;fontFamily=Helvetica;html=1;whiteSpace=wrap;" vertex="1" parent="WIyWlLk6GJQsqaUBKTNV-1">
<mxGeometry x="-880" y="260" width="320" height="340" as="geometry" />
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>