Update Java 并发.md
重新绘制配图,补充 AQS、乐观锁与悲观锁、Java 中的锁、读写锁和 CompletableFuture,更新线程池、中断和先行发生原则等描述。
@@ -0,0 +1,63 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="780" height="368" viewBox="0 0 780 368">
|
||||
<style>
|
||||
text { font-family: "PingFang SC", "Microsoft YaHei", Helvetica, Arial, sans-serif; fill: #3F3F3F; font-size: 12px; }
|
||||
.title { font-weight: bold; font-size: 15px; fill: #333; text-anchor: middle; }
|
||||
.b { font-weight: bold; }
|
||||
.c { text-anchor: middle; }
|
||||
.e { text-anchor: end; }
|
||||
.sub { font-size: 11px; fill: #6E6E6E; }
|
||||
.note { fill: #C0605A; }
|
||||
.ok { fill: #6A9F68; }
|
||||
.mono { font-family: Menlo, Consolas, monospace; }
|
||||
.arrow { stroke: #555; stroke-width: 1.1; fill: none; marker-end: url(#a); }
|
||||
.red { stroke: #C0605A; marker-end: url(#r); }
|
||||
.green { stroke: #6A9F68; marker-end: url(#g); }
|
||||
.dash { stroke-dasharray: 5 4; }
|
||||
.line { stroke: #555; stroke-width: 1.1; fill: none; }
|
||||
.frame { fill: none; stroke: #999; stroke-width: 1; stroke-dasharray: 4 3; }
|
||||
.shadow { fill: #DADADA; }
|
||||
</style>
|
||||
<defs>
|
||||
<marker id="a" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#555"/>
|
||||
</marker>
|
||||
<marker id="r" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#C0605A"/>
|
||||
</marker>
|
||||
<marker id="g" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#6A9F68"/>
|
||||
</marker>
|
||||
</defs>
|
||||
<rect width="100%" height="100%" fill="#FFFFFF"/>
|
||||
|
||||
<text x="390" y="28" class="title">AtomicInteger:CAS 失败就重新读取再试</text>
|
||||
<rect x="40" y="50" width="335" height="34" rx="6" fill="#C6E4FB" stroke="#7FB2D6"/>
|
||||
<text x="207.5" y="65" class="c sub b">线程 T1</text>
|
||||
<text x="207.5" y="80" class="sub c">incrementAndGet()</text>
|
||||
<line x1="207.5" y1="84" x2="207.5" y2="316" stroke="#D2D2D2" stroke-dasharray="4 3"/>
|
||||
<rect x="405" y="50" width="335" height="34" rx="6" fill="#C6ECE9" stroke="#7DBCB7"/>
|
||||
<text x="572.5" y="65" class="c sub b">线程 T2</text>
|
||||
<text x="572.5" y="80" class="sub c">incrementAndGet()</text>
|
||||
<line x1="572.5" y1="84" x2="572.5" y2="316" stroke="#D2D2D2" stroke-dasharray="4 3"/>
|
||||
<text x="22" y="104" class="c sub">时间</text>
|
||||
<line x1="22" y1="110" x2="22" y2="310" class="arrow"/>
|
||||
<rect x="46" y="96" width="323" height="26" rx="5" fill="#FFFFFF" stroke="#7FB2D6"/>
|
||||
<text x="56" y="113" class="sub mono" style="font-size:10px;fill:#3F3F3F">读取当前值</text>
|
||||
<text x="359" y="113" class="e sub mono" style="font-size:10px">→ 0</text>
|
||||
<rect x="411" y="132" width="323" height="26" rx="5" fill="#FFFFFF" stroke="#7DBCB7"/>
|
||||
<text x="421" y="149" class="sub mono" style="font-size:10px;fill:#3F3F3F">读取当前值</text>
|
||||
<text x="724" y="149" class="e sub mono" style="font-size:10px">→ 0</text>
|
||||
<rect x="46" y="168" width="323" height="26" rx="5" fill="#FFFFFF" stroke="#7FB2D6"/>
|
||||
<text x="56" y="185" class="sub mono" style="font-size:10px;fill:#3F3F3F">CAS(期望 0,新值 1)</text>
|
||||
<text x="359" y="185" class="e sub mono ok b" style="font-size:10px">成功</text>
|
||||
<rect x="411" y="204" width="323" height="26" rx="5" fill="#FFFFFF" stroke="#7DBCB7"/>
|
||||
<text x="421" y="221" class="sub mono" style="font-size:10px;fill:#3F3F3F">CAS(期望 0,新值 1)</text>
|
||||
<text x="724" y="221" class="e sub mono note b" style="font-size:10px">失败,当前是 1</text>
|
||||
<rect x="411" y="240" width="323" height="26" rx="5" fill="#FFFFFF" stroke="#7DBCB7"/>
|
||||
<text x="421" y="257" class="sub mono" style="font-size:10px;fill:#3F3F3F">重新读取当前值</text>
|
||||
<text x="724" y="257" class="e sub mono" style="font-size:10px">→ 1</text>
|
||||
<rect x="411" y="276" width="323" height="26" rx="5" fill="#FFFFFF" stroke="#7DBCB7"/>
|
||||
<text x="421" y="293" class="sub mono" style="font-size:10px;fill:#3F3F3F">CAS(期望 1,新值 2)</text>
|
||||
<text x="724" y="293" class="e sub mono ok b" style="font-size:10px">成功</text>
|
||||
<text x="30" y="336" class="sub b" style="fill:#3F3F3F">结果为 2。CAS 由 CPU 的一条原子指令完成:只有当前值等于期望值时才写入新值。</text>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 4.1 KiB |
@@ -0,0 +1,64 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="780" height="390" viewBox="0 0 780 390">
|
||||
<style>
|
||||
text { font-family: "PingFang SC", "Microsoft YaHei", Helvetica, Arial, sans-serif; fill: #3F3F3F; font-size: 12px; }
|
||||
.title { font-weight: bold; font-size: 15px; fill: #333; text-anchor: middle; }
|
||||
.b { font-weight: bold; }
|
||||
.c { text-anchor: middle; }
|
||||
.e { text-anchor: end; }
|
||||
.sub { font-size: 11px; fill: #6E6E6E; }
|
||||
.note { fill: #C0605A; }
|
||||
.ok { fill: #6A9F68; }
|
||||
.mono { font-family: Menlo, Consolas, monospace; }
|
||||
.arrow { stroke: #555; stroke-width: 1.1; fill: none; marker-end: url(#a); }
|
||||
.red { stroke: #C0605A; marker-end: url(#r); }
|
||||
.green { stroke: #6A9F68; marker-end: url(#g); }
|
||||
.dash { stroke-dasharray: 5 4; }
|
||||
.line { stroke: #555; stroke-width: 1.1; fill: none; }
|
||||
.frame { fill: none; stroke: #999; stroke-width: 1; stroke-dasharray: 4 3; }
|
||||
.shadow { fill: #DADADA; }
|
||||
</style>
|
||||
<defs>
|
||||
<marker id="a" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#555"/>
|
||||
</marker>
|
||||
<marker id="r" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#C0605A"/>
|
||||
</marker>
|
||||
<marker id="g" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#6A9F68"/>
|
||||
</marker>
|
||||
</defs>
|
||||
<rect width="100%" height="100%" fill="#FFFFFF"/>
|
||||
|
||||
<text x="390" y="28" class="title">两个线程同时执行 cnt++,结果只加了 1</text>
|
||||
<rect x="40" y="50" width="335" height="34" rx="6" fill="#C6E4FB" stroke="#7FB2D6"/>
|
||||
<text x="207.5" y="65" class="c sub b">线程 T1</text>
|
||||
<text x="207.5" y="80" class="sub c">cnt++</text>
|
||||
<line x1="207.5" y1="84" x2="207.5" y2="316" stroke="#D2D2D2" stroke-dasharray="4 3"/>
|
||||
<rect x="405" y="50" width="335" height="34" rx="6" fill="#C6ECE9" stroke="#7DBCB7"/>
|
||||
<text x="572.5" y="65" class="c sub b">线程 T2</text>
|
||||
<text x="572.5" y="80" class="sub c">cnt++</text>
|
||||
<line x1="572.5" y1="84" x2="572.5" y2="316" stroke="#D2D2D2" stroke-dasharray="4 3"/>
|
||||
<text x="22" y="104" class="c sub">时间</text>
|
||||
<line x1="22" y1="110" x2="22" y2="310" class="arrow"/>
|
||||
<rect x="46" y="96" width="323" height="26" rx="5" fill="#FFFFFF" stroke="#7FB2D6"/>
|
||||
<text x="56" y="113" class="sub mono" style="font-size:10px;fill:#3F3F3F">load:从主内存读入 cnt</text>
|
||||
<text x="359" y="113" class="e sub mono" style="font-size:10px">→ 0</text>
|
||||
<rect x="411" y="132" width="323" height="26" rx="5" fill="#FFFFFF" stroke="#7DBCB7"/>
|
||||
<text x="421" y="149" class="sub mono" style="font-size:10px;fill:#3F3F3F">load:从主内存读入 cnt</text>
|
||||
<text x="724" y="149" class="e sub mono" style="font-size:10px">→ 0</text>
|
||||
<rect x="46" y="168" width="323" height="26" rx="5" fill="#FFFFFF" stroke="#7FB2D6"/>
|
||||
<text x="56" y="185" class="sub mono" style="font-size:10px;fill:#3F3F3F">assign:cnt = 0 + 1</text>
|
||||
<text x="359" y="185" class="e sub mono" style="font-size:10px">→ 1</text>
|
||||
<rect x="411" y="204" width="323" height="26" rx="5" fill="#FFFFFF" stroke="#7DBCB7"/>
|
||||
<text x="421" y="221" class="sub mono" style="font-size:10px;fill:#3F3F3F">assign:cnt = 0 + 1</text>
|
||||
<text x="724" y="221" class="e sub mono" style="font-size:10px">→ 1</text>
|
||||
<rect x="46" y="240" width="323" height="26" rx="5" fill="#FFFFFF" stroke="#7FB2D6"/>
|
||||
<text x="56" y="257" class="sub mono" style="font-size:10px;fill:#3F3F3F">store:写回主内存</text>
|
||||
<text x="359" y="257" class="e sub mono" style="font-size:10px">cnt = 1</text>
|
||||
<rect x="411" y="276" width="323" height="26" rx="5" fill="#FFFFFF" stroke="#7DBCB7"/>
|
||||
<text x="421" y="293" class="sub mono" style="font-size:10px;fill:#3F3F3F">store:写回主内存</text>
|
||||
<text x="724" y="293" class="e sub mono note b" style="font-size:10px">cnt = 1</text>
|
||||
<text x="30" y="336" class="sub b" style="fill:#3F3F3F">执行了两次自增,主内存中的 cnt 是 1 而不是 2。</text>
|
||||
<text x="30" y="358" class="sub" style="">load、assign、store 每一步都是原子的,但三步合在一起不是。</text>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 4.2 KiB |
@@ -0,0 +1,68 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="760" height="330" viewBox="0 0 760 330">
|
||||
<style>
|
||||
text { font-family: "PingFang SC", "Microsoft YaHei", Helvetica, Arial, sans-serif; fill: #3F3F3F; font-size: 12px; }
|
||||
.title { font-weight: bold; font-size: 15px; fill: #333; text-anchor: middle; }
|
||||
.b { font-weight: bold; }
|
||||
.c { text-anchor: middle; }
|
||||
.e { text-anchor: end; }
|
||||
.sub { font-size: 11px; fill: #6E6E6E; }
|
||||
.note { fill: #C0605A; }
|
||||
.ok { fill: #6A9F68; }
|
||||
.mono { font-family: Menlo, Consolas, monospace; }
|
||||
.arrow { stroke: #555; stroke-width: 1.1; fill: none; marker-end: url(#a); }
|
||||
.red { stroke: #C0605A; marker-end: url(#r); }
|
||||
.green { stroke: #6A9F68; marker-end: url(#g); }
|
||||
.dash { stroke-dasharray: 5 4; }
|
||||
.line { stroke: #555; stroke-width: 1.1; fill: none; }
|
||||
.frame { fill: none; stroke: #999; stroke-width: 1; stroke-dasharray: 4 3; }
|
||||
.shadow { fill: #DADADA; }
|
||||
</style>
|
||||
<defs>
|
||||
<marker id="a" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#555"/>
|
||||
</marker>
|
||||
<marker id="r" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#C0605A"/>
|
||||
</marker>
|
||||
<marker id="g" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#6A9F68"/>
|
||||
</marker>
|
||||
</defs>
|
||||
<rect width="100%" height="100%" fill="#FFFFFF"/>
|
||||
|
||||
<text x="380" y="28" class="title">CountDownLatch:计数减到 0 时唤醒等待的线程</text>
|
||||
<text x="24" y="104" class="sub mono b">main</text>
|
||||
<line x1="110" y1="100" x2="730" y2="100" stroke="#E2E2E2"/>
|
||||
<text x="24" y="164" class="sub mono b">worker-1</text>
|
||||
<line x1="110" y1="160" x2="730" y2="160" stroke="#E2E2E2"/>
|
||||
<text x="24" y="214" class="sub mono b">worker-2</text>
|
||||
<line x1="110" y1="210" x2="730" y2="210" stroke="#E2E2E2"/>
|
||||
<text x="24" y="264" class="sub mono b">worker-3</text>
|
||||
<line x1="110" y1="260" x2="730" y2="260" stroke="#E2E2E2"/>
|
||||
<rect x="120" y="91" width="50" height="18" rx="4" fill="#C6E4FB" stroke="#7FB2D6"/>
|
||||
<text x="145" y="104" class="c sub" style="font-size:10.5px">执行</text>
|
||||
<rect x="170" y="91" width="390" height="18" rx="4" fill="#FFFFFF" stroke="#7FB2D6" stroke-dasharray="4 3"/>
|
||||
<text x="365" y="104" class="c sub" style="font-size:10.5px">await(),等待</text>
|
||||
<rect x="560" y="91" width="160" height="18" rx="4" fill="#C6E4FB" stroke="#7FB2D6"/>
|
||||
<text x="640" y="104" class="c sub" style="font-size:10.5px">被唤醒,继续执行</text>
|
||||
<rect x="130" y="151" width="170" height="18" rx="4" fill="#C6ECE9" stroke="#7DBCB7"/>
|
||||
<text x="215" y="164" class="c sub" style="font-size:10.5px">执行任务</text>
|
||||
<circle cx="300" cy="160" r="5" fill="#C0605A"/>
|
||||
<text x="308" y="148" class="sub mono">countDown()</text>
|
||||
<line x1="300" y1="151" x2="300" y2="70" stroke="#C0605A" stroke-dasharray="2 3"/>
|
||||
<rect x="130" y="201" width="300" height="18" rx="4" fill="#C6ECE9" stroke="#7DBCB7"/>
|
||||
<text x="280" y="214" class="c sub" style="font-size:10.5px">执行任务</text>
|
||||
<circle cx="430" cy="210" r="5" fill="#C0605A"/>
|
||||
<text x="438" y="198" class="sub mono">countDown()</text>
|
||||
<line x1="430" y1="201" x2="430" y2="70" stroke="#C0605A" stroke-dasharray="2 3"/>
|
||||
<rect x="130" y="251" width="430" height="18" rx="4" fill="#C6ECE9" stroke="#7DBCB7"/>
|
||||
<text x="345" y="264" class="c sub" style="font-size:10.5px">执行任务</text>
|
||||
<circle cx="560" cy="260" r="5" fill="#C0605A"/>
|
||||
<text x="568" y="248" class="sub mono">countDown()</text>
|
||||
<line x1="560" y1="251" x2="560" y2="70" stroke="#C0605A" stroke-dasharray="2 3"/>
|
||||
<text x="150" y="62" class="c sub mono b" style="fill:#766AA8">count = 3</text>
|
||||
<text x="365" y="62" class="c sub mono b" style="fill:#766AA8">2</text>
|
||||
<text x="495" y="62" class="c sub mono b" style="fill:#766AA8">1</text>
|
||||
<text x="600" y="62" class="c sub mono b" style="fill:#766AA8">0</text>
|
||||
<text x="24" y="304" class="sub">示意:new CountDownLatch(3),main 调用 await() 等待 3 个工作线程各调用一次 countDown()。计数不能重置,只能使用一次。</text>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 4.2 KiB |
@@ -0,0 +1,79 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="760" height="330" viewBox="0 0 760 330">
|
||||
<style>
|
||||
text { font-family: "PingFang SC", "Microsoft YaHei", Helvetica, Arial, sans-serif; fill: #3F3F3F; font-size: 12px; }
|
||||
.title { font-weight: bold; font-size: 15px; fill: #333; text-anchor: middle; }
|
||||
.b { font-weight: bold; }
|
||||
.c { text-anchor: middle; }
|
||||
.e { text-anchor: end; }
|
||||
.sub { font-size: 11px; fill: #6E6E6E; }
|
||||
.note { fill: #C0605A; }
|
||||
.ok { fill: #6A9F68; }
|
||||
.mono { font-family: Menlo, Consolas, monospace; }
|
||||
.arrow { stroke: #555; stroke-width: 1.1; fill: none; marker-end: url(#a); }
|
||||
.red { stroke: #C0605A; marker-end: url(#r); }
|
||||
.green { stroke: #6A9F68; marker-end: url(#g); }
|
||||
.dash { stroke-dasharray: 5 4; }
|
||||
.line { stroke: #555; stroke-width: 1.1; fill: none; }
|
||||
.frame { fill: none; stroke: #999; stroke-width: 1; stroke-dasharray: 4 3; }
|
||||
.shadow { fill: #DADADA; }
|
||||
</style>
|
||||
<defs>
|
||||
<marker id="a" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#555"/>
|
||||
</marker>
|
||||
<marker id="r" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#C0605A"/>
|
||||
</marker>
|
||||
<marker id="g" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#6A9F68"/>
|
||||
</marker>
|
||||
</defs>
|
||||
<rect width="100%" height="100%" fill="#FFFFFF"/>
|
||||
|
||||
<text x="380" y="28" class="title">CPU 缓存:每个核心先读写自己的缓存</text>
|
||||
<rect x="40" y="60" width="100" height="34" rx="6" fill="#C6E4FB" stroke="#7FB2D6"/>
|
||||
<text x="90" y="81.5" class="c sub b">核心 0</text>
|
||||
<rect x="40" y="104" width="100" height="40" rx="6" fill="#C6ECE9" stroke="#7DBCB7"/>
|
||||
<text x="90" y="122" class="b c">L1 数据缓存</text>
|
||||
<text x="90" y="137" class="sub c">128 KB</text>
|
||||
<line x1="90" y1="94" x2="90" y2="104" class="arrow "/>
|
||||
<rect x="160" y="60" width="100" height="34" rx="6" fill="#C6E4FB" stroke="#7FB2D6"/>
|
||||
<text x="210" y="81.5" class="c sub b">核心 1</text>
|
||||
<rect x="160" y="104" width="100" height="40" rx="6" fill="#C6ECE9" stroke="#7DBCB7"/>
|
||||
<text x="210" y="122" class="b c">L1 数据缓存</text>
|
||||
<text x="210" y="137" class="sub c">128 KB</text>
|
||||
<line x1="210" y1="94" x2="210" y2="104" class="arrow "/>
|
||||
<rect x="280" y="60" width="100" height="34" rx="6" fill="#C6E4FB" stroke="#7FB2D6"/>
|
||||
<text x="330" y="81.5" class="c sub b">核心 2</text>
|
||||
<rect x="280" y="104" width="100" height="40" rx="6" fill="#C6ECE9" stroke="#7DBCB7"/>
|
||||
<text x="330" y="122" class="b c">L1 数据缓存</text>
|
||||
<text x="330" y="137" class="sub c">128 KB</text>
|
||||
<line x1="330" y1="94" x2="330" y2="104" class="arrow "/>
|
||||
<rect x="400" y="60" width="100" height="34" rx="6" fill="#C6E4FB" stroke="#7FB2D6"/>
|
||||
<text x="450" y="81.5" class="c sub b">核心 3</text>
|
||||
<rect x="400" y="104" width="100" height="40" rx="6" fill="#C6ECE9" stroke="#7DBCB7"/>
|
||||
<text x="450" y="122" class="b c">L1 数据缓存</text>
|
||||
<text x="450" y="137" class="sub c">128 KB</text>
|
||||
<line x1="450" y1="94" x2="450" y2="104" class="arrow "/>
|
||||
<rect x="40" y="168" width="460" height="40" rx="6" fill="#EAE9E7" stroke="#BFBEBA"/>
|
||||
<text x="270" y="186" class="b c">L2 缓存,4 个性能核共享</text>
|
||||
<text x="270" y="201" class="sub c">12 MB</text>
|
||||
<line x1="90" y1="144" x2="90" y2="168" class="arrow "/>
|
||||
<line x1="210" y1="144" x2="210" y2="168" class="arrow "/>
|
||||
<line x1="330" y1="144" x2="330" y2="168" class="arrow "/>
|
||||
<line x1="450" y1="144" x2="450" y2="168" class="arrow "/>
|
||||
<rect x="40" y="236" width="460" height="40" rx="6" fill="#EAE9E7" stroke="#BFBEBA"/>
|
||||
<text x="270" y="254" class="b c">主内存</text>
|
||||
<text x="270" y="269" class="sub c">32 GB</text>
|
||||
<line x1="270" y1="208" x2="270" y2="236" class="arrow "/>
|
||||
<text x="530" y="82" class="sub b">以本机 Apple M1 Pro 为例(sysctl)</text>
|
||||
<text x="530" y="106" class="sub">缓存以缓存行为单位读写,</text>
|
||||
<text x="530" y="124" class="sub">本机的缓存行为 128 字节。</text>
|
||||
<text x="530" y="154" class="sub">核心 0 修改了缓存中的变量,</text>
|
||||
<text x="530" y="172" class="sub">其它核心缓存里可能还是旧值,</text>
|
||||
<text x="530" y="190" class="sub">缓存一致性协议负责让它们失效,</text>
|
||||
<text x="530" y="208" class="sub">但编译器和 CPU 的重排序、</text>
|
||||
<text x="530" y="226" class="sub">写缓冲仍会导致其它线程看不到</text>
|
||||
<text x="530" y="244" class="sub">最新的值。</text>
|
||||
<text x="24" y="310" class="sub">图中只画了一组 4 个性能核;本机共有 8 个性能核和 2 个能效核。</text>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 4.8 KiB |
@@ -0,0 +1,68 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="760" height="330" viewBox="0 0 760 330">
|
||||
<style>
|
||||
text { font-family: "PingFang SC", "Microsoft YaHei", Helvetica, Arial, sans-serif; fill: #3F3F3F; font-size: 12px; }
|
||||
.title { font-weight: bold; font-size: 15px; fill: #333; text-anchor: middle; }
|
||||
.b { font-weight: bold; }
|
||||
.c { text-anchor: middle; }
|
||||
.e { text-anchor: end; }
|
||||
.sub { font-size: 11px; fill: #6E6E6E; }
|
||||
.note { fill: #C0605A; }
|
||||
.ok { fill: #6A9F68; }
|
||||
.mono { font-family: Menlo, Consolas, monospace; }
|
||||
.arrow { stroke: #555; stroke-width: 1.1; fill: none; marker-end: url(#a); }
|
||||
.red { stroke: #C0605A; marker-end: url(#r); }
|
||||
.green { stroke: #6A9F68; marker-end: url(#g); }
|
||||
.dash { stroke-dasharray: 5 4; }
|
||||
.line { stroke: #555; stroke-width: 1.1; fill: none; }
|
||||
.frame { fill: none; stroke: #999; stroke-width: 1; stroke-dasharray: 4 3; }
|
||||
.shadow { fill: #DADADA; }
|
||||
</style>
|
||||
<defs>
|
||||
<marker id="a" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#555"/>
|
||||
</marker>
|
||||
<marker id="r" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#C0605A"/>
|
||||
</marker>
|
||||
<marker id="g" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#6A9F68"/>
|
||||
</marker>
|
||||
</defs>
|
||||
<rect width="100%" height="100%" fill="#FFFFFF"/>
|
||||
|
||||
<text x="380" y="28" class="title">CyclicBarrier:线程到齐后一起继续,然后自动开始下一轮</text>
|
||||
<text x="24" y="114" class="sub mono b">thread-0</text>
|
||||
<line x1="100" y1="110" x2="730" y2="110" stroke="#E2E2E2"/>
|
||||
<text x="24" y="164" class="sub mono b">thread-1</text>
|
||||
<line x1="100" y1="160" x2="730" y2="160" stroke="#E2E2E2"/>
|
||||
<text x="24" y="214" class="sub mono b">thread-2</text>
|
||||
<line x1="100" y1="210" x2="730" y2="210" stroke="#E2E2E2"/>
|
||||
<rect x="110" y="101" width="120" height="18" rx="4" fill="#C6E4FB" stroke="#7FB2D6"/>
|
||||
<text x="170" y="114" class="c sub" style="font-size:10.5px">第 1 轮</text>
|
||||
<rect x="230" y="101" width="120" height="18" rx="4" fill="#FFFFFF" stroke="#7FB2D6" stroke-dasharray="4 3"/>
|
||||
<text x="290" y="114" class="c sub" style="font-size:10.5px">await()</text>
|
||||
<rect x="354" y="101" width="120" height="18" rx="4" fill="#C6ECE9" stroke="#7DBCB7"/>
|
||||
<text x="414" y="114" class="c sub" style="font-size:10.5px">第 2 轮</text>
|
||||
<rect x="474" y="101" width="120" height="18" rx="4" fill="#FFFFFF" stroke="#7DBCB7" stroke-dasharray="4 3"/>
|
||||
<text x="534" y="114" class="c sub" style="font-size:10.5px">await()</text>
|
||||
<rect x="110" y="151" width="180" height="18" rx="4" fill="#C6E4FB" stroke="#7FB2D6"/>
|
||||
<text x="200" y="164" class="c sub" style="font-size:10.5px">第 1 轮</text>
|
||||
<rect x="290" y="151" width="60" height="18" rx="4" fill="#FFFFFF" stroke="#7FB2D6" stroke-dasharray="4 3"/>
|
||||
<text x="320" y="164" class="c sub" style="font-size:10.5px">await()</text>
|
||||
<rect x="354" y="151" width="180" height="18" rx="4" fill="#C6ECE9" stroke="#7DBCB7"/>
|
||||
<text x="444" y="164" class="c sub" style="font-size:10.5px">第 2 轮</text>
|
||||
<rect x="534" y="151" width="60" height="18" rx="4" fill="#FFFFFF" stroke="#7DBCB7" stroke-dasharray="4 3"/>
|
||||
<text x="564" y="164" class="c sub" style="font-size:10.5px">await()</text>
|
||||
<rect x="110" y="201" width="240" height="18" rx="4" fill="#C6E4FB" stroke="#7FB2D6"/>
|
||||
<text x="230" y="214" class="c sub" style="font-size:10.5px">第 1 轮</text>
|
||||
<rect x="354" y="201" width="240" height="18" rx="4" fill="#C6ECE9" stroke="#7DBCB7"/>
|
||||
<text x="474" y="214" class="c sub" style="font-size:10.5px">第 2 轮</text>
|
||||
<line x1="350" y1="80" x2="350" y2="232" stroke="#766AA8" stroke-width="2"/>
|
||||
<text x="350" y="74" class="c sub b" style="fill:#766AA8">屏障打开</text>
|
||||
<text x="350" y="250" class="c sub">执行 barrierAction</text>
|
||||
<line x1="594" y1="80" x2="594" y2="232" stroke="#766AA8" stroke-width="2"/>
|
||||
<text x="594" y="74" class="c sub b" style="fill:#766AA8">再次打开</text>
|
||||
<text x="594" y="250" class="c sub">执行 barrierAction</text>
|
||||
<text x="24" y="286" class="sub">new CyclicBarrier(3, barrierAction):先到的线程在 await() 处等待,第 3 个线程到达时屏障打开;</text>
|
||||
<text x="24" y="304" class="sub">计数随即恢复为 3,同一个对象可以用于下一轮。在 JDK 21 上运行两轮,barrierAction 各执行了一次。</text>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 4.5 KiB |
@@ -0,0 +1,50 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="600" height="300" viewBox="0 0 600 300">
|
||||
<style>
|
||||
text { font-family: "PingFang SC", "Microsoft YaHei", Helvetica, Arial, sans-serif; fill: #3F3F3F; font-size: 12px; }
|
||||
.title { font-weight: bold; font-size: 15px; fill: #333; text-anchor: middle; }
|
||||
.b { font-weight: bold; }
|
||||
.c { text-anchor: middle; }
|
||||
.e { text-anchor: end; }
|
||||
.sub { font-size: 11px; fill: #6E6E6E; }
|
||||
.note { fill: #C0605A; }
|
||||
.ok { fill: #6A9F68; }
|
||||
.mono { font-family: Menlo, Consolas, monospace; }
|
||||
.arrow { stroke: #555; stroke-width: 1.1; fill: none; marker-end: url(#a); }
|
||||
.red { stroke: #C0605A; marker-end: url(#r); }
|
||||
.green { stroke: #6A9F68; marker-end: url(#g); }
|
||||
.dash { stroke-dasharray: 5 4; }
|
||||
.line { stroke: #555; stroke-width: 1.1; fill: none; }
|
||||
.frame { fill: none; stroke: #999; stroke-width: 1; stroke-dasharray: 4 3; }
|
||||
.shadow { fill: #DADADA; }
|
||||
</style>
|
||||
<defs>
|
||||
<marker id="a" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#555"/>
|
||||
</marker>
|
||||
<marker id="r" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#C0605A"/>
|
||||
</marker>
|
||||
<marker id="g" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#6A9F68"/>
|
||||
</marker>
|
||||
</defs>
|
||||
<rect width="100%" height="100%" fill="#FFFFFF"/>
|
||||
|
||||
<text x="300" y="28" class="title">线程加入规则</text>
|
||||
<rect x="60" y="50" width="220" height="30" rx="6" fill="#C6E4FB" stroke="#7FB2D6"/>
|
||||
<text x="170" y="69.5" class="c sub b">线程 t</text>
|
||||
<rect x="340" y="50" width="220" height="30" rx="6" fill="#C6ECE9" stroke="#7DBCB7"/>
|
||||
<text x="450" y="69.5" class="c sub b">main</text>
|
||||
<rect x="70" y="96" width="200" height="26" rx="5" fill="#FFFFFF" stroke="#7FB2D6"/>
|
||||
<text x="170" y="113" class="c sub mono" style="font-size:10.5px;fill:#3F3F3F">x = 1;</text>
|
||||
<rect x="70" y="136" width="200" height="26" rx="5" fill="#FFFFFF" stroke="#7FB2D6"/>
|
||||
<text x="170" y="153" class="c sub mono" style="font-size:10.5px;fill:#3F3F3F">// 线程结束</text>
|
||||
<rect x="350" y="176" width="200" height="26" rx="5" fill="#FFFFFF" stroke="#7DBCB7"/>
|
||||
<text x="450" y="193" class="c sub mono" style="font-size:10.5px;fill:#3F3F3F">t.join(); // 返回</text>
|
||||
<rect x="350" y="216" width="200" height="26" rx="5" fill="#FFFFFF" stroke="#7DBCB7"/>
|
||||
<text x="450" y="233" class="c sub mono" style="font-size:10.5px;fill:#3F3F3F">print(x); // 1</text>
|
||||
<line x1="170" y1="122" x2="170" y2="136" class="arrow green"/>
|
||||
<path d="M270,149 C310,149 310,189 348,189" class="arrow green"/>
|
||||
<line x1="450" y1="202" x2="450" y2="216" class="arrow green"/>
|
||||
<text x="24" y="284" class="sub">线程 t 中的所有操作先行发生于其它线程从 t.join() 返回。</text>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 3.0 KiB |
@@ -0,0 +1,51 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="600" height="340" viewBox="0 0 600 340">
|
||||
<style>
|
||||
text { font-family: "PingFang SC", "Microsoft YaHei", Helvetica, Arial, sans-serif; fill: #3F3F3F; font-size: 12px; }
|
||||
.title { font-weight: bold; font-size: 15px; fill: #333; text-anchor: middle; }
|
||||
.b { font-weight: bold; }
|
||||
.c { text-anchor: middle; }
|
||||
.e { text-anchor: end; }
|
||||
.sub { font-size: 11px; fill: #6E6E6E; }
|
||||
.note { fill: #C0605A; }
|
||||
.ok { fill: #6A9F68; }
|
||||
.mono { font-family: Menlo, Consolas, monospace; }
|
||||
.arrow { stroke: #555; stroke-width: 1.1; fill: none; marker-end: url(#a); }
|
||||
.red { stroke: #C0605A; marker-end: url(#r); }
|
||||
.green { stroke: #6A9F68; marker-end: url(#g); }
|
||||
.dash { stroke-dasharray: 5 4; }
|
||||
.line { stroke: #555; stroke-width: 1.1; fill: none; }
|
||||
.frame { fill: none; stroke: #999; stroke-width: 1; stroke-dasharray: 4 3; }
|
||||
.shadow { fill: #DADADA; }
|
||||
</style>
|
||||
<defs>
|
||||
<marker id="a" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#555"/>
|
||||
</marker>
|
||||
<marker id="r" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#C0605A"/>
|
||||
</marker>
|
||||
<marker id="g" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#6A9F68"/>
|
||||
</marker>
|
||||
</defs>
|
||||
<rect width="100%" height="100%" fill="#FFFFFF"/>
|
||||
|
||||
<text x="300" y="28" class="title">管程锁定规则</text>
|
||||
<rect x="60" y="50" width="220" height="30" rx="6" fill="#C6E4FB" stroke="#7FB2D6"/>
|
||||
<text x="170" y="69.5" class="c sub b">线程 T1</text>
|
||||
<rect x="340" y="50" width="220" height="30" rx="6" fill="#C6ECE9" stroke="#7DBCB7"/>
|
||||
<text x="450" y="69.5" class="c sub b">线程 T2</text>
|
||||
<rect x="70" y="96" width="200" height="26" rx="5" fill="#FFFFFF" stroke="#7FB2D6"/>
|
||||
<text x="170" y="113" class="c sub mono" style="font-size:10.5px;fill:#3F3F3F">synchronized (lock) {</text>
|
||||
<rect x="70" y="136" width="200" height="26" rx="5" fill="#FFFFFF" stroke="#7FB2D6"/>
|
||||
<text x="170" y="153" class="c sub mono" style="font-size:10.5px;fill:#3F3F3F">x = 1;</text>
|
||||
<rect x="70" y="176" width="200" height="26" rx="5" fill="#FFFFFF" stroke="#7FB2D6"/>
|
||||
<text x="170" y="193" class="c sub mono" style="font-size:10.5px;fill:#3F3F3F">} // unlock</text>
|
||||
<rect x="350" y="216" width="200" height="26" rx="5" fill="#FFFFFF" stroke="#7DBCB7"/>
|
||||
<text x="450" y="233" class="c sub mono" style="font-size:10.5px;fill:#3F3F3F">synchronized (lock) { // lock</text>
|
||||
<rect x="350" y="256" width="200" height="26" rx="5" fill="#FFFFFF" stroke="#7DBCB7"/>
|
||||
<text x="450" y="273" class="c sub mono" style="font-size:10.5px;fill:#3F3F3F">print(x); // 1</text>
|
||||
<path d="M270,189 C310,189 310,229 348,229" class="arrow green"/>
|
||||
<line x1="450" y1="242" x2="450" y2="256" class="arrow green"/>
|
||||
<text x="24" y="324" class="sub">T1 释放锁先行发生于 T2 之后获得同一把锁,T1 在锁内的修改对 T2 可见。</text>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 3.1 KiB |
@@ -0,0 +1,45 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="340" height="260" viewBox="0 0 340 260">
|
||||
<style>
|
||||
text { font-family: "PingFang SC", "Microsoft YaHei", Helvetica, Arial, sans-serif; fill: #3F3F3F; font-size: 12px; }
|
||||
.title { font-weight: bold; font-size: 15px; fill: #333; text-anchor: middle; }
|
||||
.b { font-weight: bold; }
|
||||
.c { text-anchor: middle; }
|
||||
.e { text-anchor: end; }
|
||||
.sub { font-size: 11px; fill: #6E6E6E; }
|
||||
.note { fill: #C0605A; }
|
||||
.ok { fill: #6A9F68; }
|
||||
.mono { font-family: Menlo, Consolas, monospace; }
|
||||
.arrow { stroke: #555; stroke-width: 1.1; fill: none; marker-end: url(#a); }
|
||||
.red { stroke: #C0605A; marker-end: url(#r); }
|
||||
.green { stroke: #6A9F68; marker-end: url(#g); }
|
||||
.dash { stroke-dasharray: 5 4; }
|
||||
.line { stroke: #555; stroke-width: 1.1; fill: none; }
|
||||
.frame { fill: none; stroke: #999; stroke-width: 1; stroke-dasharray: 4 3; }
|
||||
.shadow { fill: #DADADA; }
|
||||
</style>
|
||||
<defs>
|
||||
<marker id="a" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#555"/>
|
||||
</marker>
|
||||
<marker id="r" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#C0605A"/>
|
||||
</marker>
|
||||
<marker id="g" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#6A9F68"/>
|
||||
</marker>
|
||||
</defs>
|
||||
<rect width="100%" height="100%" fill="#FFFFFF"/>
|
||||
|
||||
<text x="170" y="28" class="title">单一线程原则</text>
|
||||
<rect x="50" y="50" width="220" height="30" rx="6" fill="#C6E4FB" stroke="#7FB2D6"/>
|
||||
<text x="160" y="69.5" class="c sub b">线程 T</text>
|
||||
<rect x="60" y="96" width="200" height="26" rx="5" fill="#FFFFFF" stroke="#7FB2D6"/>
|
||||
<text x="160" y="113" class="c sub mono" style="font-size:10.5px;fill:#3F3F3F">x = 1;</text>
|
||||
<rect x="60" y="136" width="200" height="26" rx="5" fill="#FFFFFF" stroke="#7FB2D6"/>
|
||||
<text x="160" y="153" class="c sub mono" style="font-size:10.5px;fill:#3F3F3F">y = x + 1;</text>
|
||||
<rect x="60" y="176" width="200" height="26" rx="5" fill="#FFFFFF" stroke="#7FB2D6"/>
|
||||
<text x="160" y="193" class="c sub mono" style="font-size:10.5px;fill:#3F3F3F">print(y); // 2</text>
|
||||
<line x1="160" y1="122" x2="160" y2="136" class="arrow green"/>
|
||||
<line x1="160" y1="162" x2="160" y2="176" class="arrow green"/>
|
||||
<text x="24" y="244" class="sub">同一个线程内,前面的操作先行发生于后面的操作。</text>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 2.5 KiB |
@@ -0,0 +1,47 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="600" height="260" viewBox="0 0 600 260">
|
||||
<style>
|
||||
text { font-family: "PingFang SC", "Microsoft YaHei", Helvetica, Arial, sans-serif; fill: #3F3F3F; font-size: 12px; }
|
||||
.title { font-weight: bold; font-size: 15px; fill: #333; text-anchor: middle; }
|
||||
.b { font-weight: bold; }
|
||||
.c { text-anchor: middle; }
|
||||
.e { text-anchor: end; }
|
||||
.sub { font-size: 11px; fill: #6E6E6E; }
|
||||
.note { fill: #C0605A; }
|
||||
.ok { fill: #6A9F68; }
|
||||
.mono { font-family: Menlo, Consolas, monospace; }
|
||||
.arrow { stroke: #555; stroke-width: 1.1; fill: none; marker-end: url(#a); }
|
||||
.red { stroke: #C0605A; marker-end: url(#r); }
|
||||
.green { stroke: #6A9F68; marker-end: url(#g); }
|
||||
.dash { stroke-dasharray: 5 4; }
|
||||
.line { stroke: #555; stroke-width: 1.1; fill: none; }
|
||||
.frame { fill: none; stroke: #999; stroke-width: 1; stroke-dasharray: 4 3; }
|
||||
.shadow { fill: #DADADA; }
|
||||
</style>
|
||||
<defs>
|
||||
<marker id="a" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#555"/>
|
||||
</marker>
|
||||
<marker id="r" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#C0605A"/>
|
||||
</marker>
|
||||
<marker id="g" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#6A9F68"/>
|
||||
</marker>
|
||||
</defs>
|
||||
<rect width="100%" height="100%" fill="#FFFFFF"/>
|
||||
|
||||
<text x="300" y="28" class="title">线程启动规则</text>
|
||||
<rect x="60" y="50" width="220" height="30" rx="6" fill="#C6E4FB" stroke="#7FB2D6"/>
|
||||
<text x="170" y="69.5" class="c sub b">main</text>
|
||||
<rect x="340" y="50" width="220" height="30" rx="6" fill="#C6ECE9" stroke="#7DBCB7"/>
|
||||
<text x="450" y="69.5" class="c sub b">线程 t</text>
|
||||
<rect x="70" y="96" width="200" height="26" rx="5" fill="#FFFFFF" stroke="#7FB2D6"/>
|
||||
<text x="170" y="113" class="c sub mono" style="font-size:10.5px;fill:#3F3F3F">x = 1;</text>
|
||||
<rect x="70" y="136" width="200" height="26" rx="5" fill="#FFFFFF" stroke="#7FB2D6"/>
|
||||
<text x="170" y="153" class="c sub mono" style="font-size:10.5px;fill:#3F3F3F">t.start();</text>
|
||||
<rect x="350" y="176" width="200" height="26" rx="5" fill="#FFFFFF" stroke="#7DBCB7"/>
|
||||
<text x="450" y="193" class="c sub mono" style="font-size:10.5px;fill:#3F3F3F">print(x); // 1</text>
|
||||
<line x1="170" y1="122" x2="170" y2="136" class="arrow green"/>
|
||||
<path d="M270,149 C310,149 310,189 348,189" class="arrow green"/>
|
||||
<text x="24" y="244" class="sub">start() 先行发生于新线程中的每一个操作。</text>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 2.7 KiB |
@@ -0,0 +1,50 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="600" height="300" viewBox="0 0 600 300">
|
||||
<style>
|
||||
text { font-family: "PingFang SC", "Microsoft YaHei", Helvetica, Arial, sans-serif; fill: #3F3F3F; font-size: 12px; }
|
||||
.title { font-weight: bold; font-size: 15px; fill: #333; text-anchor: middle; }
|
||||
.b { font-weight: bold; }
|
||||
.c { text-anchor: middle; }
|
||||
.e { text-anchor: end; }
|
||||
.sub { font-size: 11px; fill: #6E6E6E; }
|
||||
.note { fill: #C0605A; }
|
||||
.ok { fill: #6A9F68; }
|
||||
.mono { font-family: Menlo, Consolas, monospace; }
|
||||
.arrow { stroke: #555; stroke-width: 1.1; fill: none; marker-end: url(#a); }
|
||||
.red { stroke: #C0605A; marker-end: url(#r); }
|
||||
.green { stroke: #6A9F68; marker-end: url(#g); }
|
||||
.dash { stroke-dasharray: 5 4; }
|
||||
.line { stroke: #555; stroke-width: 1.1; fill: none; }
|
||||
.frame { fill: none; stroke: #999; stroke-width: 1; stroke-dasharray: 4 3; }
|
||||
.shadow { fill: #DADADA; }
|
||||
</style>
|
||||
<defs>
|
||||
<marker id="a" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#555"/>
|
||||
</marker>
|
||||
<marker id="r" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#C0605A"/>
|
||||
</marker>
|
||||
<marker id="g" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#6A9F68"/>
|
||||
</marker>
|
||||
</defs>
|
||||
<rect width="100%" height="100%" fill="#FFFFFF"/>
|
||||
|
||||
<text x="300" y="28" class="title">volatile 变量规则</text>
|
||||
<rect x="60" y="50" width="220" height="30" rx="6" fill="#C6E4FB" stroke="#7FB2D6"/>
|
||||
<text x="170" y="69.5" class="c sub b">线程 T1</text>
|
||||
<rect x="340" y="50" width="220" height="30" rx="6" fill="#C6ECE9" stroke="#7DBCB7"/>
|
||||
<text x="450" y="69.5" class="c sub b">线程 T2</text>
|
||||
<rect x="70" y="96" width="200" height="26" rx="5" fill="#FFFFFF" stroke="#7FB2D6"/>
|
||||
<text x="170" y="113" class="c sub mono" style="font-size:10.5px;fill:#3F3F3F">data = 42;</text>
|
||||
<rect x="70" y="136" width="200" height="26" rx="5" fill="#FFFFFF" stroke="#7FB2D6"/>
|
||||
<text x="170" y="153" class="c sub mono" style="font-size:10.5px;fill:#3F3F3F">ready = true; // volatile</text>
|
||||
<rect x="350" y="176" width="200" height="26" rx="5" fill="#FFFFFF" stroke="#7DBCB7"/>
|
||||
<text x="450" y="193" class="c sub mono" style="font-size:10.5px;fill:#3F3F3F">if (ready) // 读到 true</text>
|
||||
<rect x="350" y="216" width="200" height="26" rx="5" fill="#FFFFFF" stroke="#7DBCB7"/>
|
||||
<text x="450" y="233" class="c sub mono" style="font-size:10.5px;fill:#3F3F3F">print(data); // 42</text>
|
||||
<line x1="170" y1="122" x2="170" y2="136" class="arrow green"/>
|
||||
<path d="M270,149 C310,149 310,189 348,189" class="arrow green"/>
|
||||
<line x1="450" y1="202" x2="450" y2="216" class="arrow green"/>
|
||||
<text x="24" y="284" class="sub">写 volatile 变量先行发生于之后读到这个值;加上传递性,T2 也能看到 data = 42。</text>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 3.0 KiB |
@@ -0,0 +1,54 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="760" height="300" viewBox="0 0 760 300">
|
||||
<style>
|
||||
text { font-family: "PingFang SC", "Microsoft YaHei", Helvetica, Arial, sans-serif; fill: #3F3F3F; font-size: 12px; }
|
||||
.title { font-weight: bold; font-size: 15px; fill: #333; text-anchor: middle; }
|
||||
.b { font-weight: bold; }
|
||||
.c { text-anchor: middle; }
|
||||
.e { text-anchor: end; }
|
||||
.sub { font-size: 11px; fill: #6E6E6E; }
|
||||
.note { fill: #C0605A; }
|
||||
.ok { fill: #6A9F68; }
|
||||
.mono { font-family: Menlo, Consolas, monospace; }
|
||||
.arrow { stroke: #555; stroke-width: 1.1; fill: none; marker-end: url(#a); }
|
||||
.red { stroke: #C0605A; marker-end: url(#r); }
|
||||
.green { stroke: #6A9F68; marker-end: url(#g); }
|
||||
.dash { stroke-dasharray: 5 4; }
|
||||
.line { stroke: #555; stroke-width: 1.1; fill: none; }
|
||||
.frame { fill: none; stroke: #999; stroke-width: 1; stroke-dasharray: 4 3; }
|
||||
.shadow { fill: #DADADA; }
|
||||
</style>
|
||||
<defs>
|
||||
<marker id="a" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#555"/>
|
||||
</marker>
|
||||
<marker id="r" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#C0605A"/>
|
||||
</marker>
|
||||
<marker id="g" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#6A9F68"/>
|
||||
</marker>
|
||||
</defs>
|
||||
<rect width="100%" height="100%" fill="#FFFFFF"/>
|
||||
|
||||
<text x="380" y="28" class="title">主内存与工作内存之间的 8 种操作</text>
|
||||
<rect x="30" y="90" width="170" height="120" rx="6" fill="#EAE9E7" stroke="#BFBEBA"/>
|
||||
<text x="115" y="148" class="b c">主内存</text>
|
||||
<text x="115" y="163" class="sub c">变量 cnt</text>
|
||||
<rect x="300" y="90" width="170" height="120" rx="6" fill="#C6E4FB" stroke="#7FB2D6"/>
|
||||
<text x="385" y="148" class="b c">工作内存</text>
|
||||
<text x="385" y="163" class="sub c">cnt 的副本</text>
|
||||
<rect x="570" y="90" width="160" height="120" rx="6" fill="#C6ECE9" stroke="#7DBCB7"/>
|
||||
<text x="650" y="148" class="b c">执行引擎</text>
|
||||
<text x="650" y="163" class="sub c">cnt + 1</text>
|
||||
<line x1="200" y1="118" x2="300" y2="118" class="arrow "/>
|
||||
<text x="250" y="110" class="c sub mono">read → load</text>
|
||||
<line x1="300" y1="182" x2="200" y2="182" class="arrow "/>
|
||||
<text x="250" y="200" class="c sub mono">store → write</text>
|
||||
<line x1="470" y1="118" x2="570" y2="118" class="arrow "/>
|
||||
<text x="520" y="110" class="c sub mono">use</text>
|
||||
<line x1="570" y1="182" x2="470" y2="182" class="arrow "/>
|
||||
<text x="520" y="200" class="c sub mono">assign</text>
|
||||
<text x="115" y="236" class="c sub mono b">lock / unlock</text>
|
||||
<text x="115" y="252" class="c sub">作用于主内存中的变量</text>
|
||||
<text x="24" y="284" class="sub">read 把值从主内存传到工作内存,load 放入副本;store 把副本传回主内存,write 写入变量。</text>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 3.0 KiB |
@@ -0,0 +1,61 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="760" height="300" viewBox="0 0 760 300">
|
||||
<style>
|
||||
text { font-family: "PingFang SC", "Microsoft YaHei", Helvetica, Arial, sans-serif; fill: #3F3F3F; font-size: 12px; }
|
||||
.title { font-weight: bold; font-size: 15px; fill: #333; text-anchor: middle; }
|
||||
.b { font-weight: bold; }
|
||||
.c { text-anchor: middle; }
|
||||
.e { text-anchor: end; }
|
||||
.sub { font-size: 11px; fill: #6E6E6E; }
|
||||
.note { fill: #C0605A; }
|
||||
.ok { fill: #6A9F68; }
|
||||
.mono { font-family: Menlo, Consolas, monospace; }
|
||||
.arrow { stroke: #555; stroke-width: 1.1; fill: none; marker-end: url(#a); }
|
||||
.red { stroke: #C0605A; marker-end: url(#r); }
|
||||
.green { stroke: #6A9F68; marker-end: url(#g); }
|
||||
.dash { stroke-dasharray: 5 4; }
|
||||
.line { stroke: #555; stroke-width: 1.1; fill: none; }
|
||||
.frame { fill: none; stroke: #999; stroke-width: 1; stroke-dasharray: 4 3; }
|
||||
.shadow { fill: #DADADA; }
|
||||
</style>
|
||||
<defs>
|
||||
<marker id="a" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#555"/>
|
||||
</marker>
|
||||
<marker id="r" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#C0605A"/>
|
||||
</marker>
|
||||
<marker id="g" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#6A9F68"/>
|
||||
</marker>
|
||||
</defs>
|
||||
<rect width="100%" height="100%" fill="#FFFFFF"/>
|
||||
|
||||
<text x="380" y="28" class="title">Java 内存模型:线程读写自己的工作内存,通过主内存交换数据</text>
|
||||
<rect x="30" y="60" width="110" height="44" rx="6" fill="#C6ECE9" stroke="#7DBCB7"/>
|
||||
<text x="85" y="86.5" class="c sub b">线程 1</text>
|
||||
<rect x="220" y="60" width="170" height="44" rx="6" fill="#E6E3FA" stroke="#B8B0DE"/>
|
||||
<text x="305" y="80" class="b c">工作内存</text>
|
||||
<text x="305" y="95" class="sub c">cnt = 1</text>
|
||||
<line x1="140" y1="82" x2="220" y2="82" stroke="#888" stroke-dasharray="4 3"/>
|
||||
<line x1="390" y1="82" x2="520" y2="162" stroke="#888" stroke-dasharray="4 3"/>
|
||||
<text x="305" y="120" class="c sub note">已修改,还没写回</text>
|
||||
<rect x="30" y="130" width="110" height="44" rx="6" fill="#C6ECE9" stroke="#7DBCB7"/>
|
||||
<text x="85" y="156.5" class="c sub b">线程 2</text>
|
||||
<rect x="220" y="130" width="170" height="44" rx="6" fill="#C6E4FB" stroke="#7FB2D6"/>
|
||||
<text x="305" y="150" class="b c">工作内存</text>
|
||||
<text x="305" y="165" class="sub c">cnt = 0</text>
|
||||
<line x1="140" y1="152" x2="220" y2="152" stroke="#888" stroke-dasharray="4 3"/>
|
||||
<line x1="390" y1="152" x2="520" y2="162" stroke="#888" stroke-dasharray="4 3"/>
|
||||
<text x="305" y="190" class="c sub ">读到的是旧值</text>
|
||||
<rect x="30" y="200" width="110" height="44" rx="6" fill="#C6ECE9" stroke="#7DBCB7"/>
|
||||
<text x="85" y="226.5" class="c sub b">线程 3</text>
|
||||
<rect x="220" y="200" width="170" height="44" rx="6" fill="#C6E4FB" stroke="#7FB2D6"/>
|
||||
<text x="305" y="220" class="b c">工作内存</text>
|
||||
<text x="305" y="235" class="sub c">cnt = 0</text>
|
||||
<line x1="140" y1="222" x2="220" y2="222" stroke="#888" stroke-dasharray="4 3"/>
|
||||
<line x1="390" y1="222" x2="520" y2="162" stroke="#888" stroke-dasharray="4 3"/>
|
||||
<rect x="520" y="140" width="190" height="44" rx="6" fill="#EAE9E7" stroke="#BFBEBA"/>
|
||||
<text x="615" y="160" class="b c">主内存</text>
|
||||
<text x="615" y="175" class="sub c">cnt = 0</text>
|
||||
<text x="24" y="280" class="sub">工作内存是对寄存器、缓存等的抽象。线程 1 把 cnt 改为 1 但还没有写回主内存,线程 2 读到的仍然是 0。</text>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 3.7 KiB |
@@ -0,0 +1,73 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="760" height="270" viewBox="0 0 760 270">
|
||||
<style>
|
||||
text { font-family: "PingFang SC", "Microsoft YaHei", Helvetica, Arial, sans-serif; fill: #3F3F3F; font-size: 12px; }
|
||||
.title { font-weight: bold; font-size: 15px; fill: #333; text-anchor: middle; }
|
||||
.b { font-weight: bold; }
|
||||
.c { text-anchor: middle; }
|
||||
.e { text-anchor: end; }
|
||||
.sub { font-size: 11px; fill: #6E6E6E; }
|
||||
.note { fill: #C0605A; }
|
||||
.ok { fill: #6A9F68; }
|
||||
.mono { font-family: Menlo, Consolas, monospace; }
|
||||
.arrow { stroke: #555; stroke-width: 1.1; fill: none; marker-end: url(#a); }
|
||||
.red { stroke: #C0605A; marker-end: url(#r); }
|
||||
.green { stroke: #6A9F68; marker-end: url(#g); }
|
||||
.dash { stroke-dasharray: 5 4; }
|
||||
.line { stroke: #555; stroke-width: 1.1; fill: none; }
|
||||
.frame { fill: none; stroke: #999; stroke-width: 1; stroke-dasharray: 4 3; }
|
||||
.shadow { fill: #DADADA; }
|
||||
</style>
|
||||
<defs>
|
||||
<marker id="a" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#555"/>
|
||||
</marker>
|
||||
<marker id="r" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#C0605A"/>
|
||||
</marker>
|
||||
<marker id="g" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#6A9F68"/>
|
||||
</marker>
|
||||
</defs>
|
||||
<rect width="100%" height="100%" fill="#FFFFFF"/>
|
||||
|
||||
<text x="380" y="28" class="title">ThreadLocal:值保存在每个线程自己的 ThreadLocalMap 中</text>
|
||||
<rect x="30" y="56" width="260" height="136" rx="8" class="frame"/>
|
||||
<text x="40" y="74" class="b">Thread1.threadLocals</text>
|
||||
<rect x="50" y="88" width="110" height="26" rx="3" fill="#EAE9E7" stroke="#BFBEBA" stroke-width="1"/>
|
||||
<text x="105" y="105" class="c sub" style="fill:#444">value</text>
|
||||
<rect x="50" y="114" width="110" height="30" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
|
||||
<text x="105" y="133" class="c sub mono" style="fill:#444">1</text>
|
||||
<rect x="50" y="144" width="110" height="30" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
|
||||
<text x="105" y="163" class="c sub mono" style="fill:#444">1</text>
|
||||
<rect x="160" y="88" width="110" height="26" rx="3" fill="#EAE9E7" stroke="#BFBEBA" stroke-width="1"/>
|
||||
<text x="215" y="105" class="c sub" style="fill:#444">key(弱引用)</text>
|
||||
<rect x="160" y="114" width="110" height="30" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
|
||||
<text x="215" y="133" class="c sub mono" style="fill:#444">threadLocal1</text>
|
||||
<rect x="160" y="144" width="110" height="30" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
|
||||
<text x="215" y="163" class="c sub mono" style="fill:#444">threadLocal2</text>
|
||||
<rect x="470" y="56" width="260" height="136" rx="8" class="frame"/>
|
||||
<text x="480" y="74" class="b">Thread2.threadLocals</text>
|
||||
<rect x="490" y="88" width="110" height="26" rx="3" fill="#EAE9E7" stroke="#BFBEBA" stroke-width="1"/>
|
||||
<text x="545" y="105" class="c sub" style="fill:#444">key(弱引用)</text>
|
||||
<rect x="490" y="114" width="110" height="30" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
|
||||
<text x="545" y="133" class="c sub mono" style="fill:#444">threadLocal1</text>
|
||||
<rect x="490" y="144" width="110" height="30" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
|
||||
<text x="545" y="163" class="c sub mono" style="fill:#444">threadLocal2</text>
|
||||
<rect x="600" y="88" width="110" height="26" rx="3" fill="#EAE9E7" stroke="#BFBEBA" stroke-width="1"/>
|
||||
<text x="655" y="105" class="c sub" style="fill:#444">value</text>
|
||||
<rect x="600" y="114" width="110" height="30" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
|
||||
<text x="655" y="133" class="c sub mono" style="fill:#444">2</text>
|
||||
<rect x="600" y="144" width="110" height="30" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
|
||||
<text x="655" y="163" class="c sub mono" style="fill:#444">2</text>
|
||||
<rect x="320" y="116" width="120" height="26" rx="6" fill="#E6E3FA" stroke="#B8B0DE"/>
|
||||
<text x="380" y="133.5" class="c sub mono b">threadLocal1</text>
|
||||
<line x1="270" y1="129" x2="318" y2="129" class="arrow dash"/>
|
||||
<line x1="490" y1="129" x2="442" y2="129" class="arrow dash"/>
|
||||
<rect x="320" y="146" width="120" height="26" rx="6" fill="#E6E3FA" stroke="#B8B0DE"/>
|
||||
<text x="380" y="163.5" class="c sub mono b">threadLocal2</text>
|
||||
<line x1="270" y1="159" x2="318" y2="159" class="arrow dash"/>
|
||||
<line x1="490" y1="159" x2="442" y2="159" class="arrow dash"/>
|
||||
<text x="380" y="100" class="c sub b">ThreadLocal 对象</text>
|
||||
<text x="24" y="226" class="sub">每个线程的 Map 以 ThreadLocal 对象为 key:同一个 threadLocal1,在两个线程中取到的值不同。</text>
|
||||
<text x="24" y="246" class="sub">Entry 对 key 是弱引用,对 value 是强引用;线程池中的线程一直存活,value 不 remove() 就一直留在 Map 中。</text>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 5.0 KiB |
@@ -0,0 +1,53 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="760" height="300" viewBox="0 0 760 300">
|
||||
<style>
|
||||
text { font-family: "PingFang SC", "Microsoft YaHei", Helvetica, Arial, sans-serif; fill: #3F3F3F; font-size: 12px; }
|
||||
.title { font-weight: bold; font-size: 15px; fill: #333; text-anchor: middle; }
|
||||
.b { font-weight: bold; }
|
||||
.c { text-anchor: middle; }
|
||||
.e { text-anchor: end; }
|
||||
.sub { font-size: 11px; fill: #6E6E6E; }
|
||||
.note { fill: #C0605A; }
|
||||
.ok { fill: #6A9F68; }
|
||||
.mono { font-family: Menlo, Consolas, monospace; }
|
||||
.arrow { stroke: #555; stroke-width: 1.1; fill: none; marker-end: url(#a); }
|
||||
.red { stroke: #C0605A; marker-end: url(#r); }
|
||||
.green { stroke: #6A9F68; marker-end: url(#g); }
|
||||
.dash { stroke-dasharray: 5 4; }
|
||||
.line { stroke: #555; stroke-width: 1.1; fill: none; }
|
||||
.frame { fill: none; stroke: #999; stroke-width: 1; stroke-dasharray: 4 3; }
|
||||
.shadow { fill: #DADADA; }
|
||||
</style>
|
||||
<defs>
|
||||
<marker id="a" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#555"/>
|
||||
</marker>
|
||||
<marker id="r" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#C0605A"/>
|
||||
</marker>
|
||||
<marker id="g" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#6A9F68"/>
|
||||
</marker>
|
||||
</defs>
|
||||
<rect width="100%" height="100%" fill="#FFFFFF"/>
|
||||
|
||||
<text x="380" y="28" class="title">工作窃取:自己从队列一端取,窃取从另一端取</text>
|
||||
<text x="220" y="70" class="c sub b">Thread1 的双端队列</text>
|
||||
<rect x="60" y="82" width="320" height="50" rx="6" fill="#FFFFFF" stroke="#BFBEBA"/>
|
||||
<rect x="72" y="92" width="92" height="30" rx="3" fill="#E6E3FA" stroke="#B8B0DE" stroke-width="1"/>
|
||||
<text x="118" y="111" class="c sub mono" style="fill:#444">Task1</text>
|
||||
<rect x="174" y="92" width="92" height="30" rx="3" fill="#C6E4FB" stroke="#7FB2D6" stroke-width="1"/>
|
||||
<text x="220" y="111" class="c sub mono" style="fill:#444">Task2</text>
|
||||
<rect x="276" y="92" width="92" height="30" rx="3" fill="#C6E4FB" stroke="#7FB2D6" stroke-width="1"/>
|
||||
<text x="322" y="111" class="c sub mono" style="fill:#444">Task3</text>
|
||||
<text x="60" y="150" class="sub b">旧</text>
|
||||
<text x="380" y="150" class="e sub b">新</text>
|
||||
<line x1="382" y1="107" x2="470" y2="107" class="arrow "/>
|
||||
<text x="480" y="103" class="sub">Thread1 自己取出最新的 Task3</text>
|
||||
<text x="480" y="121" class="sub">(后进先出)</text>
|
||||
<rect x="480" y="190" width="200" height="40" rx="6" fill="#C6ECE9" stroke="#7DBCB7"/>
|
||||
<text x="580" y="214.5" class="c sub b">Thread2 的队列为空</text>
|
||||
<path d="M118,122 C118,200 300,214 476,210" class="arrow green"/>
|
||||
<text x="220" y="222" class="c sub ok">Thread2 窃取最早的 Task1(先进先出)</text>
|
||||
<text x="24" y="266" class="sub">所有者和窃取者在队列的两端操作,只有队列中只剩一个任务时才会发生竞争。</text>
|
||||
<text x="24" y="284" class="sub">先放入的任务通常是拆分前更大的任务,窃取它能让空闲线程分到更多的工作。</text>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 3.2 KiB |
@@ -0,0 +1,52 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="720" height="250" viewBox="0 0 720 250">
|
||||
<style>
|
||||
text { font-family: "PingFang SC", "Microsoft YaHei", Helvetica, Arial, sans-serif; fill: #3F3F3F; font-size: 12px; }
|
||||
.title { font-weight: bold; font-size: 15px; fill: #333; text-anchor: middle; }
|
||||
.b { font-weight: bold; }
|
||||
.c { text-anchor: middle; }
|
||||
.e { text-anchor: end; }
|
||||
.sub { font-size: 11px; fill: #6E6E6E; }
|
||||
.note { fill: #C0605A; }
|
||||
.ok { fill: #6A9F68; }
|
||||
.mono { font-family: Menlo, Consolas, monospace; }
|
||||
.arrow { stroke: #555; stroke-width: 1.1; fill: none; marker-end: url(#a); }
|
||||
.red { stroke: #C0605A; marker-end: url(#r); }
|
||||
.green { stroke: #6A9F68; marker-end: url(#g); }
|
||||
.dash { stroke-dasharray: 5 4; }
|
||||
.line { stroke: #555; stroke-width: 1.1; fill: none; }
|
||||
.frame { fill: none; stroke: #999; stroke-width: 1; stroke-dasharray: 4 3; }
|
||||
.shadow { fill: #DADADA; }
|
||||
</style>
|
||||
<defs>
|
||||
<marker id="a" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#555"/>
|
||||
</marker>
|
||||
<marker id="r" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#C0605A"/>
|
||||
</marker>
|
||||
<marker id="g" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#6A9F68"/>
|
||||
</marker>
|
||||
</defs>
|
||||
<rect width="100%" height="100%" fill="#FFFFFF"/>
|
||||
|
||||
<text x="360" y="28" class="title">轻量级锁:加锁就是一次 CAS</text>
|
||||
<rect x="30" y="50" width="250" height="160" rx="8" class="frame"/>
|
||||
<text x="40" y="68" class="b">线程 T1 的栈</text>
|
||||
<rect x="50" y="80" width="210" height="30" rx="6" fill="#EAE9E7" stroke="#BFBEBA"/>
|
||||
<text x="155" y="99.5" class="c sub">其它栈帧</text>
|
||||
<rect x="50" y="120" width="210" height="60" rx="6" fill="#C6E4FB" stroke="#7FB2D6"/>
|
||||
<text x="155" y="140" class="c sub b" style="fill:#444">Lock Record</text>
|
||||
<text x="155" y="160" class="c sub">保存原来的 Mark Word …01</text>
|
||||
<rect x="400" y="50" width="290" height="160" rx="8" class="frame"/>
|
||||
<text x="410" y="68" class="b">锁对象</text>
|
||||
<rect x="420" y="80" width="250" height="34" rx="6" fill="#E6E3FA" stroke="#B8B0DE"/>
|
||||
<text x="545" y="102" class="c sub" style="fill:#444">Mark Word:Lock Record 的地址 …00</text>
|
||||
<rect x="420" y="122" width="250" height="30" rx="6" fill="#EAE9E7" stroke="#BFBEBA"/>
|
||||
<text x="545" y="141.5" class="c sub">类型指针</text>
|
||||
<rect x="420" y="158" width="250" height="30" rx="6" fill="#FFFFFF" stroke="#BBBBBB"/>
|
||||
<text x="545" y="177.5" class="c sub">字段</text>
|
||||
<line x1="418" y1="97" x2="262" y2="150" class="arrow "/>
|
||||
<text x="340" y="108" class="c sub ok">CAS 成功</text>
|
||||
<text x="30" y="236" class="sub">CAS 期望 Mark Word 仍是原来的 …01,成功就加锁完成;失败说明已被其它线程持有(或是当前线程重入),竞争时膨胀为重量级锁。</text>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 3.1 KiB |
@@ -0,0 +1,59 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="760" height="330" viewBox="0 0 760 330">
|
||||
<style>
|
||||
text { font-family: "PingFang SC", "Microsoft YaHei", Helvetica, Arial, sans-serif; fill: #3F3F3F; font-size: 12px; }
|
||||
.title { font-weight: bold; font-size: 15px; fill: #333; text-anchor: middle; }
|
||||
.b { font-weight: bold; }
|
||||
.c { text-anchor: middle; }
|
||||
.e { text-anchor: end; }
|
||||
.sub { font-size: 11px; fill: #6E6E6E; }
|
||||
.note { fill: #C0605A; }
|
||||
.ok { fill: #6A9F68; }
|
||||
.mono { font-family: Menlo, Consolas, monospace; }
|
||||
.arrow { stroke: #555; stroke-width: 1.1; fill: none; marker-end: url(#a); }
|
||||
.red { stroke: #C0605A; marker-end: url(#r); }
|
||||
.green { stroke: #6A9F68; marker-end: url(#g); }
|
||||
.dash { stroke-dasharray: 5 4; }
|
||||
.line { stroke: #555; stroke-width: 1.1; fill: none; }
|
||||
.frame { fill: none; stroke: #999; stroke-width: 1; stroke-dasharray: 4 3; }
|
||||
.shadow { fill: #DADADA; }
|
||||
</style>
|
||||
<defs>
|
||||
<marker id="a" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#555"/>
|
||||
</marker>
|
||||
<marker id="r" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#C0605A"/>
|
||||
</marker>
|
||||
<marker id="g" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#6A9F68"/>
|
||||
</marker>
|
||||
</defs>
|
||||
<rect width="100%" height="100%" fill="#FFFFFF"/>
|
||||
|
||||
<text x="380" y="28" class="title">synchronized 的锁状态变化(JDK 21 用 JOL 实测 Mark Word)</text>
|
||||
<rect x="30" y="60" width="220" height="92" rx="6" fill="#C6ECE9" stroke="#7DBCB7"/>
|
||||
<text x="140" y="84" class="c b" style="fill:#444">无锁</text>
|
||||
<text x="140" y="108" class="c sub mono" style="fill:#444">0x0000000000000001</text>
|
||||
<text x="140" y="132" class="c sub">末尾 01</text>
|
||||
<rect x="270" y="60" width="220" height="92" rx="6" fill="#C6E4FB" stroke="#7FB2D6"/>
|
||||
<text x="380" y="84" class="c b" style="fill:#444">轻量级锁</text>
|
||||
<text x="380" y="108" class="c sub mono" style="fill:#444">0x000000016faceae0</text>
|
||||
<text x="380" y="132" class="c sub">末尾 00</text>
|
||||
<text x="380" y="146" class="c sub">指向加锁线程栈中的 Lock Record</text>
|
||||
<rect x="510" y="60" width="220" height="92" rx="6" fill="#E6E3FA" stroke="#B8B0DE"/>
|
||||
<text x="620" y="84" class="c b" style="fill:#444">重量级锁</text>
|
||||
<text x="620" y="108" class="c sub mono" style="fill:#444">0x000000015b630a72</text>
|
||||
<text x="620" y="132" class="c sub">末尾 10</text>
|
||||
<text x="620" y="146" class="c sub">指向 ObjectMonitor</text>
|
||||
<line x1="252" y1="92" x2="268" y2="92" class="arrow "/>
|
||||
<line x1="268" y1="120" x2="252" y2="120" class="arrow "/>
|
||||
<text x="260" y="176" class="c sub">进入 synchronized:CAS 把 Mark Word 换成</text>
|
||||
<text x="260" y="192" class="c sub">Lock Record 地址;退出时 CAS 换回</text>
|
||||
<line x1="492" y1="106" x2="508" y2="106" class="arrow "/>
|
||||
<text x="510" y="176" class="sub">另一个线程加锁时 CAS 失败,</text>
|
||||
<text x="510" y="192" class="sub">膨胀为重量级锁,抢不到的线程阻塞</text>
|
||||
<text x="30" y="236" class="sub">实测:① 新建的对象是 0x…01;② 一个线程进入 synchronized 后变为 thin lock;③ 退出后恢复为 0x…01;</text>
|
||||
<text x="30" y="254" class="sub">④ 另一个线程持有锁时再去加锁,变为 fat lock;⑤ 两个线程都退出后仍是 fat lock,空闲的监视器由 JVM 之后回收。</text>
|
||||
<text x="30" y="278" class="sub">如果对象调用过 hashCode(),哈希值会写进 Mark Word,例如 0x00000028ac3dc301 中的 28ac3dc3。</text>
|
||||
<text x="30" y="302" class="sub">锁只会从轻量级膨胀为重量级,不会在使用中降级;JDK 15 之前,一个线程反复加锁时还会先进入偏向锁。</text>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 3.9 KiB |
@@ -0,0 +1,79 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="760" height="300" viewBox="0 0 760 300">
|
||||
<style>
|
||||
text { font-family: "PingFang SC", "Microsoft YaHei", Helvetica, Arial, sans-serif; fill: #3F3F3F; font-size: 12px; }
|
||||
.title { font-weight: bold; font-size: 15px; fill: #333; text-anchor: middle; }
|
||||
.b { font-weight: bold; }
|
||||
.c { text-anchor: middle; }
|
||||
.e { text-anchor: end; }
|
||||
.sub { font-size: 11px; fill: #6E6E6E; }
|
||||
.note { fill: #C0605A; }
|
||||
.ok { fill: #6A9F68; }
|
||||
.mono { font-family: Menlo, Consolas, monospace; }
|
||||
.arrow { stroke: #555; stroke-width: 1.1; fill: none; marker-end: url(#a); }
|
||||
.red { stroke: #C0605A; marker-end: url(#r); }
|
||||
.green { stroke: #6A9F68; marker-end: url(#g); }
|
||||
.dash { stroke-dasharray: 5 4; }
|
||||
.line { stroke: #555; stroke-width: 1.1; fill: none; }
|
||||
.frame { fill: none; stroke: #999; stroke-width: 1; stroke-dasharray: 4 3; }
|
||||
.shadow { fill: #DADADA; }
|
||||
</style>
|
||||
<defs>
|
||||
<marker id="a" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#555"/>
|
||||
</marker>
|
||||
<marker id="r" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#C0605A"/>
|
||||
</marker>
|
||||
<marker id="g" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#6A9F68"/>
|
||||
</marker>
|
||||
</defs>
|
||||
<rect width="100%" height="100%" fill="#FFFFFF"/>
|
||||
|
||||
<text x="380" y="28" class="title">64 位 HotSpot 的 Mark Word(JDK 8)</text>
|
||||
<text x="695" y="58" class="c sub" style="fill:#766AA8">是否偏向</text>
|
||||
<text x="721" y="72" class="c sub" style="fill:#207773">锁标志</text>
|
||||
<text x="110" y="99" class="sub b e" style="fill:#444">无锁</text>
|
||||
<rect x="120" y="80" width="219.643" height="28" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
|
||||
<text x="229.821" y="98" class="c sub" style="fill:#444">unused 25</text>
|
||||
<rect x="339.643" y="80" width="272.357" height="28" rx="3" fill="#C6E4FB" stroke="#7FB2D6" stroke-width="1"/>
|
||||
<text x="475.821" y="98" class="c sub" style="fill:#444">hashCode 31</text>
|
||||
<rect x="612" y="80" width="26" height="28" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
|
||||
<text x="625" y="98" class="c sub" style="fill:#444">1</text>
|
||||
<rect x="638" y="80" width="44" height="28" rx="3" fill="#EAE9E7" stroke="#BFBEBA" stroke-width="1"/>
|
||||
<text x="660" y="98" class="c sub" style="fill:#444">age 4</text>
|
||||
<rect x="682" y="80" width="26" height="28" rx="3" fill="#E6E3FA" stroke="#B8B0DE" stroke-width="1"/>
|
||||
<text x="695" y="98" class="c sub" style="fill:#444">0</text>
|
||||
<rect x="708" y="80" width="26" height="28" rx="3" fill="#C6ECE9" stroke="#7DBCB7" stroke-width="1"/>
|
||||
<text x="721" y="98" class="c sub" style="fill:#444">01</text>
|
||||
<text x="110" y="133" class="sub b e" style="fill:#444">偏向锁</text>
|
||||
<rect x="120" y="114" width="448" height="28" rx="3" fill="#C6E4FB" stroke="#7FB2D6" stroke-width="1"/>
|
||||
<text x="344" y="132" class="c sub" style="fill:#444">线程 ID 54</text>
|
||||
<rect x="568" y="114" width="44" height="28" rx="3" fill="#EAE9E7" stroke="#BFBEBA" stroke-width="1"/>
|
||||
<text x="590" y="132" class="c sub" style="fill:#444">epoch 2</text>
|
||||
<rect x="612" y="114" width="26" height="28" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
|
||||
<text x="625" y="132" class="c sub" style="fill:#444">1</text>
|
||||
<rect x="638" y="114" width="44" height="28" rx="3" fill="#EAE9E7" stroke="#BFBEBA" stroke-width="1"/>
|
||||
<text x="660" y="132" class="c sub" style="fill:#444">age 4</text>
|
||||
<rect x="682" y="114" width="26" height="28" rx="3" fill="#E6E3FA" stroke="#B8B0DE" stroke-width="1"/>
|
||||
<text x="695" y="132" class="c sub" style="fill:#444">1</text>
|
||||
<rect x="708" y="114" width="26" height="28" rx="3" fill="#C6ECE9" stroke="#7DBCB7" stroke-width="1"/>
|
||||
<text x="721" y="132" class="c sub" style="fill:#444">01</text>
|
||||
<text x="110" y="167" class="sub b e" style="fill:#444">轻量级锁</text>
|
||||
<rect x="120" y="148" width="588" height="28" rx="3" fill="#C6E4FB" stroke="#7FB2D6" stroke-width="1"/>
|
||||
<text x="414" y="166" class="c sub" style="fill:#444">指向栈中 Lock Record 的指针 62</text>
|
||||
<rect x="708" y="148" width="26" height="28" rx="3" fill="#C6ECE9" stroke="#7DBCB7" stroke-width="1"/>
|
||||
<text x="721" y="166" class="c sub" style="fill:#444">00</text>
|
||||
<text x="110" y="201" class="sub b e" style="fill:#444">重量级锁</text>
|
||||
<rect x="120" y="182" width="588" height="28" rx="3" fill="#C6E4FB" stroke="#7FB2D6" stroke-width="1"/>
|
||||
<text x="414" y="200" class="c sub" style="fill:#444">指向 ObjectMonitor 的指针 62</text>
|
||||
<rect x="708" y="182" width="26" height="28" rx="3" fill="#C6ECE9" stroke="#7DBCB7" stroke-width="1"/>
|
||||
<text x="721" y="200" class="c sub" style="fill:#444">10</text>
|
||||
<text x="110" y="235" class="sub b e" style="fill:#444">GC 标记</text>
|
||||
<rect x="120" y="216" width="588" height="28" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
|
||||
<text x="414" y="234" class="c sub" style="fill:#444"></text>
|
||||
<rect x="708" y="216" width="26" height="28" rx="3" fill="#C6ECE9" stroke="#7DBCB7" stroke-width="1"/>
|
||||
<text x="721" y="234" class="c sub" style="fill:#444">11</text>
|
||||
<text x="30" y="268" class="sub">末尾两位是锁标志,01 时再看倒数第 3 位区分无锁和偏向锁。偏向锁在 JDK 15 默认禁用(JEP 374),之后被移除,</text>
|
||||
<text x="30" y="286" class="sub">现在对象只在无锁、轻量级锁、重量级锁之间转换。依据 jdk8u 的 markOop.hpp。</text>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 5.6 KiB |