Java并发包AQS队列实现原理
一、文章概述
AQS(AbstractQueuedSynchronizer)是Java.util.concurrent包的核心基石,为ReentrantLock、CountDownLatch、Semaphore等几乎所有并发工具提供了统一的同步实现框架。其核心思想是:用一个volatile修饰的int型state变量表示同步状态,配合一个FIFO的**CLH(Craig, Landin, and Hagersten)**双向等待队列,实现线程的排队、阻塞与唤醒机制,解决了同步器实现的重复劳动问题。
二、核心数据结构
| 组件 | 类型 | 核心作用 |
|---|---|---|
| state | volatile int | 0=无锁,>0=持有锁次数,天然支持可重入 |
| 等待队列 | CLH双向链表 | 存储所有获取锁失败的阻塞线程 |
| Node节点 | 内部类 | 封装线程引用、等待状态、前后指针 |
| 节点状态 | int常量 | SIGNAL(等待唤醒)、CANCELLED(已取消)等 |
三、获取锁核心执行流程
#mermaid-svg-zyl9Bp21Ilt7Kfmf{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-zyl9Bp21Ilt7Kfmf .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-zyl9Bp21Ilt7Kfmf .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-zyl9Bp21Ilt7Kfmf .error-icon{fill:#552222;}#mermaid-svg-zyl9Bp21Ilt7Kfmf .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-zyl9Bp21Ilt7Kfmf .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-zyl9Bp21Ilt7Kfmf .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-zyl9Bp21Ilt7Kfmf .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-zyl9Bp21Ilt7Kfmf .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-zyl9Bp21Ilt7Kfmf .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-zyl9Bp21Ilt7Kfmf .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-zyl9Bp21Ilt7Kfmf .marker{fill:#333333;stroke:#333333;}#mermaid-svg-zyl9Bp21Ilt7Kfmf .marker.cross{stroke:#333333;}#mermaid-svg-zyl9Bp21Ilt7Kfmf svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-zyl9Bp21Ilt7Kfmf p{margin:0;}#mermaid-svg-zyl9Bp21Ilt7Kfmf .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-zyl9Bp21Ilt7Kfmf .cluster-label text{fill:#333;}#mermaid-svg-zyl9Bp21Ilt7Kfmf .cluster-label span{color:#333;}#mermaid-svg-zyl9Bp21Ilt7Kfmf .cluster-label span p{background-color:transparent;}#mermaid-svg-zyl9Bp21Ilt7Kfmf .label text,#mermaid-svg-zyl9Bp21Ilt7Kfmf span{fill:#333;color:#333;}#mermaid-svg-zyl9Bp21Ilt7Kfmf .node rect,#mermaid-svg-zyl9Bp21Ilt7Kfmf .node circle,#mermaid-svg-zyl9Bp21Ilt7Kfmf .node ellipse,#mermaid-svg-zyl9Bp21Ilt7Kfmf .node polygon,#mermaid-svg-zyl9Bp21Ilt7Kfmf .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-zyl9Bp21Ilt7Kfmf .rough-node .label text,#mermaid-svg-zyl9Bp21Ilt7Kfmf .node .label text,#mermaid-svg-zyl9Bp21Ilt7Kfmf .image-shape .label,#mermaid-svg-zyl9Bp21Ilt7Kfmf .icon-shape .label{text-anchor:middle;}#mermaid-svg-zyl9Bp21Ilt7Kfmf .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-zyl9Bp21Ilt7Kfmf .rough-node .label,#mermaid-svg-zyl9Bp21Ilt7Kfmf .node .label,#mermaid-svg-zyl9Bp21Ilt7Kfmf .image-shape .label,#mermaid-svg-zyl9Bp21Ilt7Kfmf .icon-shape .label{text-align:center;}#mermaid-svg-zyl9Bp21Ilt7Kfmf .node.clickable{cursor:pointer;}#mermaid-svg-zyl9Bp21Ilt7Kfmf .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-zyl9Bp21Ilt7Kfmf .arrowheadPath{fill:#333333;}#mermaid-svg-zyl9Bp21Ilt7Kfmf .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-zyl9Bp21Ilt7Kfmf .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-zyl9Bp21Ilt7Kfmf .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-zyl9Bp21Ilt7Kfmf .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-zyl9Bp21Ilt7Kfmf .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-zyl9Bp21Ilt7Kfmf .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-zyl9Bp21Ilt7Kfmf .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-zyl9Bp21Ilt7Kfmf .cluster text{fill:#333;}#mermaid-svg-zyl9Bp21Ilt7Kfmf .cluster span{color:#333;}#mermaid-svg-zyl9Bp21Ilt7Kfmf div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-zyl9Bp21Ilt7Kfmf .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-zyl9Bp21Ilt7Kfmf rect.text{fill:none;stroke-width:0;}#mermaid-svg-zyl9Bp21Ilt7Kfmf .icon-shape,#mermaid-svg-zyl9Bp21Ilt7Kfmf .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-zyl9Bp21Ilt7Kfmf .icon-shape p,#mermaid-svg-zyl9Bp21Ilt7Kfmf .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-zyl9Bp21Ilt7Kfmf .icon-shape .label rect,#mermaid-svg-zyl9Bp21Ilt7Kfmf .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-zyl9Bp21Ilt7Kfmf .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-zyl9Bp21Ilt7Kfmf .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-zyl9Bp21Ilt7Kfmf :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 是
否
是
否
线程调用acquire
tryAcquire成功
执行同步代码
构造Node节点CAS入队
自旋检查前驱是否为头节点
再次tryAcquire成功
设为新头节点
park阻塞当前线程
被前驱节点unpark唤醒
四、节点状态流转
#mermaid-svg-63tzFOyONs2Tj9P9{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-63tzFOyONs2Tj9P9 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-63tzFOyONs2Tj9P9 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-63tzFOyONs2Tj9P9 .error-icon{fill:#552222;}#mermaid-svg-63tzFOyONs2Tj9P9 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-63tzFOyONs2Tj9P9 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-63tzFOyONs2Tj9P9 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-63tzFOyONs2Tj9P9 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-63tzFOyONs2Tj9P9 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-63tzFOyONs2Tj9P9 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-63tzFOyONs2Tj9P9 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-63tzFOyONs2Tj9P9 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-63tzFOyONs2Tj9P9 .marker.cross{stroke:#333333;}#mermaid-svg-63tzFOyONs2Tj9P9 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-63tzFOyONs2Tj9P9 p{margin:0;}#mermaid-svg-63tzFOyONs2Tj9P9 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-63tzFOyONs2Tj9P9 .cluster-label text{fill:#333;}#mermaid-svg-63tzFOyONs2Tj9P9 .cluster-label span{color:#333;}#mermaid-svg-63tzFOyONs2Tj9P9 .cluster-label span p{background-color:transparent;}#mermaid-svg-63tzFOyONs2Tj9P9 .label text,#mermaid-svg-63tzFOyONs2Tj9P9 span{fill:#333;color:#333;}#mermaid-svg-63tzFOyONs2Tj9P9 .node rect,#mermaid-svg-63tzFOyONs2Tj9P9 .node circle,#mermaid-svg-63tzFOyONs2Tj9P9 .node ellipse,#mermaid-svg-63tzFOyONs2Tj9P9 .node polygon,#mermaid-svg-63tzFOyONs2Tj9P9 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-63tzFOyONs2Tj9P9 .rough-node .label text,#mermaid-svg-63tzFOyONs2Tj9P9 .node .label text,#mermaid-svg-63tzFOyONs2Tj9P9 .image-shape .label,#mermaid-svg-63tzFOyONs2Tj9P9 .icon-shape .label{text-anchor:middle;}#mermaid-svg-63tzFOyONs2Tj9P9 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-63tzFOyONs2Tj9P9 .rough-node .label,#mermaid-svg-63tzFOyONs2Tj9P9 .node .label,#mermaid-svg-63tzFOyONs2Tj9P9 .image-shape .label,#mermaid-svg-63tzFOyONs2Tj9P9 .icon-shape .label{text-align:center;}#mermaid-svg-63tzFOyONs2Tj9P9 .node.clickable{cursor:pointer;}#mermaid-svg-63tzFOyONs2Tj9P9 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-63tzFOyONs2Tj9P9 .arrowheadPath{fill:#333333;}#mermaid-svg-63tzFOyONs2Tj9P9 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-63tzFOyONs2Tj9P9 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-63tzFOyONs2Tj9P9 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-63tzFOyONs2Tj9P9 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-63tzFOyONs2Tj9P9 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-63tzFOyONs2Tj9P9 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-63tzFOyONs2Tj9P9 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-63tzFOyONs2Tj9P9 .cluster text{fill:#333;}#mermaid-svg-63tzFOyONs2Tj9P9 .cluster span{color:#333;}#mermaid-svg-63tzFOyONs2Tj9P9 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-63tzFOyONs2Tj9P9 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-63tzFOyONs2Tj9P9 rect.text{fill:none;stroke-width:0;}#mermaid-svg-63tzFOyONs2Tj9P9 .icon-shape,#mermaid-svg-63tzFOyONs2Tj9P9 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-63tzFOyONs2Tj9P9 .icon-shape p,#mermaid-svg-63tzFOyONs2Tj9P9 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-63tzFOyONs2Tj9P9 .icon-shape .label rect,#mermaid-svg-63tzFOyONs2Tj9P9 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-63tzFOyONs2Tj9P9 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-63tzFOyONs2Tj9P9 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-63tzFOyONs2Tj9P9 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 新建节点
前驱设为SIGNAL
被唤醒并获锁
线程中断/超时
队列自动清理
初始状态
INITIAL
SIGNAL
ACQUIRED
CANCELLED
结束
五、关键设计要点
- 无锁化修改:所有对state和队列指针的操作均通过Unsafe类的CAS实现,避免了重量级锁的开销
- 精确唤醒:使用LockSupport.park/unpark替代传统wait/notify,支持单个线程精确唤醒,避免虚假唤醒
- 可扩展架构 :采用模板方法模式,子类仅需实现以下5个方法,即可自定义独占/共享同步逻辑:
tryAcquire(int):独占式获取同步状态tryRelease(int):独占式释放同步状态tryAcquireShared(int):共享式获取同步状态tryReleaseShared(int):共享式释放同步状态isHeldExclusively():判断当前线程是否独占同步状态
六、释放锁完整流程(时序图,不考虑可重入场景)
队列阻塞线程 AQS框架 持有锁线程 队列阻塞线程 AQS框架 持有锁线程 #mermaid-svg-BKNyYvL1JVE8Hx1K{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-BKNyYvL1JVE8Hx1K .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-BKNyYvL1JVE8Hx1K .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-BKNyYvL1JVE8Hx1K .error-icon{fill:#552222;}#mermaid-svg-BKNyYvL1JVE8Hx1K .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-BKNyYvL1JVE8Hx1K .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-BKNyYvL1JVE8Hx1K .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-BKNyYvL1JVE8Hx1K .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-BKNyYvL1JVE8Hx1K .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-BKNyYvL1JVE8Hx1K .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-BKNyYvL1JVE8Hx1K .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-BKNyYvL1JVE8Hx1K .marker{fill:#333333;stroke:#333333;}#mermaid-svg-BKNyYvL1JVE8Hx1K .marker.cross{stroke:#333333;}#mermaid-svg-BKNyYvL1JVE8Hx1K svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-BKNyYvL1JVE8Hx1K p{margin:0;}#mermaid-svg-BKNyYvL1JVE8Hx1K .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-BKNyYvL1JVE8Hx1K text.actor>tspan{fill:black;stroke:none;}#mermaid-svg-BKNyYvL1JVE8Hx1K .actor-line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-BKNyYvL1JVE8Hx1K .innerArc{stroke-width:1.5;stroke-dasharray:none;}#mermaid-svg-BKNyYvL1JVE8Hx1K .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-BKNyYvL1JVE8Hx1K .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-BKNyYvL1JVE8Hx1K #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-BKNyYvL1JVE8Hx1K .sequenceNumber{fill:white;}#mermaid-svg-BKNyYvL1JVE8Hx1K #sequencenumber{fill:#333;}#mermaid-svg-BKNyYvL1JVE8Hx1K #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-BKNyYvL1JVE8Hx1K .messageText{fill:#333;stroke:none;}#mermaid-svg-BKNyYvL1JVE8Hx1K .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-BKNyYvL1JVE8Hx1K .labelText,#mermaid-svg-BKNyYvL1JVE8Hx1K .labelText>tspan{fill:black;stroke:none;}#mermaid-svg-BKNyYvL1JVE8Hx1K .loopText,#mermaid-svg-BKNyYvL1JVE8Hx1K .loopText>tspan{fill:black;stroke:none;}#mermaid-svg-BKNyYvL1JVE8Hx1K .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-BKNyYvL1JVE8Hx1K .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-BKNyYvL1JVE8Hx1K .noteText,#mermaid-svg-BKNyYvL1JVE8Hx1K .noteText>tspan{fill:black;stroke:none;}#mermaid-svg-BKNyYvL1JVE8Hx1K .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-BKNyYvL1JVE8Hx1K .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-BKNyYvL1JVE8Hx1K .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-BKNyYvL1JVE8Hx1K .actorPopupMenu{position:absolute;}#mermaid-svg-BKNyYvL1JVE8Hx1K .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 / 0.4));}#mermaid-svg-BKNyYvL1JVE8Hx1K .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-BKNyYvL1JVE8Hx1K .actor-man circle,#mermaid-svg-BKNyYvL1JVE8Hx1K line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-BKNyYvL1JVE8Hx1K :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 调用release() 发起锁释放执行tryRelease(),将state置为0遍历队列,定位头节点的后继有效节点LockSupport.unpark() 唤醒阻塞线程尝试获取同步状态获取锁成功,执行同步代码
总结
AQS通过"状态变量+CLH等待队列"的极简设计,抽象了并发同步的通用逻辑,将复杂的线程调度封装在框架内部,极大降低了自定义同步器的开发难度,是Java并发编程中最经典的设计之一。