ReentrantLock公平锁实现与可重入特性
一、文章概述
ReentrantLock是JUC并发包下基于AQS实现的独占锁,支持可重入,并提供公平锁、非公平锁两种模式。本文讲解可重入实现原理,明确公平锁获取失败后的入队规则,清晰对比两种锁的核心差异。
二、核心特性
| 特性 | 说明 |
|---|---|
| 可重入性 | 同一线程可多次获取锁,通过state计数实现 |
| 公平锁 | 严格遵循队列顺序,无抢占、无重试 |
| 非公平锁 | 支持直接抢占锁,性能更高 |
三、可重入性实现原理
依托AQS的state变量实现可重入:
- 线程重入锁,
state+1 - 释放锁,
state-1 state=0代表锁完全释放
四、公平锁获取流程
#mermaid-svg-EJGSMQJ7gMmmDxzX{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-EJGSMQJ7gMmmDxzX .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-EJGSMQJ7gMmmDxzX .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-EJGSMQJ7gMmmDxzX .error-icon{fill:#552222;}#mermaid-svg-EJGSMQJ7gMmmDxzX .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-EJGSMQJ7gMmmDxzX .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-EJGSMQJ7gMmmDxzX .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-EJGSMQJ7gMmmDxzX .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-EJGSMQJ7gMmmDxzX .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-EJGSMQJ7gMmmDxzX .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-EJGSMQJ7gMmmDxzX .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-EJGSMQJ7gMmmDxzX .marker{fill:#333333;stroke:#333333;}#mermaid-svg-EJGSMQJ7gMmmDxzX .marker.cross{stroke:#333333;}#mermaid-svg-EJGSMQJ7gMmmDxzX svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-EJGSMQJ7gMmmDxzX p{margin:0;}#mermaid-svg-EJGSMQJ7gMmmDxzX .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-EJGSMQJ7gMmmDxzX .cluster-label text{fill:#333;}#mermaid-svg-EJGSMQJ7gMmmDxzX .cluster-label span{color:#333;}#mermaid-svg-EJGSMQJ7gMmmDxzX .cluster-label span p{background-color:transparent;}#mermaid-svg-EJGSMQJ7gMmmDxzX .label text,#mermaid-svg-EJGSMQJ7gMmmDxzX span{fill:#333;color:#333;}#mermaid-svg-EJGSMQJ7gMmmDxzX .node rect,#mermaid-svg-EJGSMQJ7gMmmDxzX .node circle,#mermaid-svg-EJGSMQJ7gMmmDxzX .node ellipse,#mermaid-svg-EJGSMQJ7gMmmDxzX .node polygon,#mermaid-svg-EJGSMQJ7gMmmDxzX .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-EJGSMQJ7gMmmDxzX .rough-node .label text,#mermaid-svg-EJGSMQJ7gMmmDxzX .node .label text,#mermaid-svg-EJGSMQJ7gMmmDxzX .image-shape .label,#mermaid-svg-EJGSMQJ7gMmmDxzX .icon-shape .label{text-anchor:middle;}#mermaid-svg-EJGSMQJ7gMmmDxzX .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-EJGSMQJ7gMmmDxzX .rough-node .label,#mermaid-svg-EJGSMQJ7gMmmDxzX .node .label,#mermaid-svg-EJGSMQJ7gMmmDxzX .image-shape .label,#mermaid-svg-EJGSMQJ7gMmmDxzX .icon-shape .label{text-align:center;}#mermaid-svg-EJGSMQJ7gMmmDxzX .node.clickable{cursor:pointer;}#mermaid-svg-EJGSMQJ7gMmmDxzX .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-EJGSMQJ7gMmmDxzX .arrowheadPath{fill:#333333;}#mermaid-svg-EJGSMQJ7gMmmDxzX .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-EJGSMQJ7gMmmDxzX .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-EJGSMQJ7gMmmDxzX .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-EJGSMQJ7gMmmDxzX .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-EJGSMQJ7gMmmDxzX .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-EJGSMQJ7gMmmDxzX .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-EJGSMQJ7gMmmDxzX .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-EJGSMQJ7gMmmDxzX .cluster text{fill:#333;}#mermaid-svg-EJGSMQJ7gMmmDxzX .cluster span{color:#333;}#mermaid-svg-EJGSMQJ7gMmmDxzX 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-EJGSMQJ7gMmmDxzX .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-EJGSMQJ7gMmmDxzX rect.text{fill:none;stroke-width:0;}#mermaid-svg-EJGSMQJ7gMmmDxzX .icon-shape,#mermaid-svg-EJGSMQJ7gMmmDxzX .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-EJGSMQJ7gMmmDxzX .icon-shape p,#mermaid-svg-EJGSMQJ7gMmmDxzX .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-EJGSMQJ7gMmmDxzX .icon-shape .label rect,#mermaid-svg-EJGSMQJ7gMmmDxzX .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-EJGSMQJ7gMmmDxzX .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-EJGSMQJ7gMmmDxzX .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-EJGSMQJ7gMmmDxzX :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 是
否
成功
失败
线程尝试获取锁
队列有等待线程?
加入队列队尾阻塞
CAS修改state
获取锁
被前驱唤醒
五、非公平锁获取流程
#mermaid-svg-yop23z0W6hiXhtJc{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-yop23z0W6hiXhtJc .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-yop23z0W6hiXhtJc .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-yop23z0W6hiXhtJc .error-icon{fill:#552222;}#mermaid-svg-yop23z0W6hiXhtJc .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-yop23z0W6hiXhtJc .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-yop23z0W6hiXhtJc .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-yop23z0W6hiXhtJc .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-yop23z0W6hiXhtJc .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-yop23z0W6hiXhtJc .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-yop23z0W6hiXhtJc .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-yop23z0W6hiXhtJc .marker{fill:#333333;stroke:#333333;}#mermaid-svg-yop23z0W6hiXhtJc .marker.cross{stroke:#333333;}#mermaid-svg-yop23z0W6hiXhtJc svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-yop23z0W6hiXhtJc p{margin:0;}#mermaid-svg-yop23z0W6hiXhtJc .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-yop23z0W6hiXhtJc .cluster-label text{fill:#333;}#mermaid-svg-yop23z0W6hiXhtJc .cluster-label span{color:#333;}#mermaid-svg-yop23z0W6hiXhtJc .cluster-label span p{background-color:transparent;}#mermaid-svg-yop23z0W6hiXhtJc .label text,#mermaid-svg-yop23z0W6hiXhtJc span{fill:#333;color:#333;}#mermaid-svg-yop23z0W6hiXhtJc .node rect,#mermaid-svg-yop23z0W6hiXhtJc .node circle,#mermaid-svg-yop23z0W6hiXhtJc .node ellipse,#mermaid-svg-yop23z0W6hiXhtJc .node polygon,#mermaid-svg-yop23z0W6hiXhtJc .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-yop23z0W6hiXhtJc .rough-node .label text,#mermaid-svg-yop23z0W6hiXhtJc .node .label text,#mermaid-svg-yop23z0W6hiXhtJc .image-shape .label,#mermaid-svg-yop23z0W6hiXhtJc .icon-shape .label{text-anchor:middle;}#mermaid-svg-yop23z0W6hiXhtJc .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-yop23z0W6hiXhtJc .rough-node .label,#mermaid-svg-yop23z0W6hiXhtJc .node .label,#mermaid-svg-yop23z0W6hiXhtJc .image-shape .label,#mermaid-svg-yop23z0W6hiXhtJc .icon-shape .label{text-align:center;}#mermaid-svg-yop23z0W6hiXhtJc .node.clickable{cursor:pointer;}#mermaid-svg-yop23z0W6hiXhtJc .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-yop23z0W6hiXhtJc .arrowheadPath{fill:#333333;}#mermaid-svg-yop23z0W6hiXhtJc .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-yop23z0W6hiXhtJc .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-yop23z0W6hiXhtJc .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-yop23z0W6hiXhtJc .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-yop23z0W6hiXhtJc .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-yop23z0W6hiXhtJc .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-yop23z0W6hiXhtJc .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-yop23z0W6hiXhtJc .cluster text{fill:#333;}#mermaid-svg-yop23z0W6hiXhtJc .cluster span{color:#333;}#mermaid-svg-yop23z0W6hiXhtJc 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-yop23z0W6hiXhtJc .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-yop23z0W6hiXhtJc rect.text{fill:none;stroke-width:0;}#mermaid-svg-yop23z0W6hiXhtJc .icon-shape,#mermaid-svg-yop23z0W6hiXhtJc .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-yop23z0W6hiXhtJc .icon-shape p,#mermaid-svg-yop23z0W6hiXhtJc .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-yop23z0W6hiXhtJc .icon-shape .label rect,#mermaid-svg-yop23z0W6hiXhtJc .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-yop23z0W6hiXhtJc .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-yop23z0W6hiXhtJc .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-yop23z0W6hiXhtJc :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 成功
失败
线程尝试获取锁
直接CAS抢占锁
获取锁执行代码
加入队列队尾阻塞
被唤醒
六、核心规则
- 公平锁 :CAS获取失败后,不重试、不插队,直接加入AQS队列队尾,严格遵守FIFO先来先服务
- 非公平锁:CAS抢占失败后,同样加入队列队尾,但获取锁时允许直接抢占
总结
ReentrantLock通过state实现可重入;公平锁与非公平锁获取失败后,统一加入AQS队列队尾。公平锁全程禁止插队,保证执行顺序;非公平锁允许抢占,提升吞吐量,可按需选择。