mirror of
https://github.com/CyC2018/Interview-Notebook.git
synced 2026-10-31 19:13:42 +00:00
Update Java 并发.md
补充 ThreadPoolExecutor 的参数、提交任务时的处理顺序、拒绝策略和线程数的估算方法,以及多线程交替打印,并配图。
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="700" height="360" viewBox="0 0 700 360">
|
||||
<style>
|
||||
text { font-family: "PingFang SC", "Microsoft YaHei", Helvetica, Arial, sans-serif; fill: #666; font-size: 12px; }
|
||||
.title { font-weight: bold; font-size: 15px; fill: #777; text-anchor: middle; }
|
||||
.b { font-weight: bold; }
|
||||
.c { text-anchor: middle; }
|
||||
.e { text-anchor: end; }
|
||||
.note { fill: #C0605A; }
|
||||
.ok { fill: #6A9F68; }
|
||||
.sub { font-size: 11px; fill: #888; }
|
||||
.mono { font-family: Menlo, Consolas, monospace; }
|
||||
.arrow { stroke: #666; stroke-width: 1.3; 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: #666; stroke-width: 1.3; 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 L9,5 L1,9" fill="none" stroke="#666" stroke-width="1.6"/>
|
||||
</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 L9,5 L1,9" fill="none" stroke="#C0605A" stroke-width="1.6"/>
|
||||
</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 L9,5 L1,9" fill="none" stroke="#6A9F68" stroke-width="1.6"/>
|
||||
</marker>
|
||||
</defs>
|
||||
<rect width="100%" height="100%" fill="#FFFFFF"/>
|
||||
|
||||
<text x="350" y="28" class="title">线程池处理任务的顺序</text>
|
||||
<text x="20" y="58" class="sub">核心线程数 2,最大线程数 4,队列容量 2,依次提交 7 个各执行 1 秒的任务</text>
|
||||
<text x="20" y="94" class="b">① 线程数小于核心线程数</text>
|
||||
<text x="20" y="112" class="sub">新建核心线程执行</text>
|
||||
<line x1="230" y1="98" x2="262" y2="98" class="arrow "/>
|
||||
<rect x="268" y="80" width="200" height="40" rx="6" class="shadow"/>
|
||||
<rect x="266" y="78" width="200" height="40" rx="6" fill="#B1DDF0"/>
|
||||
<text x="278" y="103" class="b">核心线程</text>
|
||||
<rect x="478" y="85" width="64" height="26" rx="4" fill="#FFFFFF" stroke="#C8C8C8"/>
|
||||
<text x="510" y="102.5" class="c sub">任务 1</text>
|
||||
<rect x="550" y="85" width="64" height="26" rx="4" fill="#FFFFFF" stroke="#C8C8C8"/>
|
||||
<text x="582" y="102.5" class="c sub">任务 2</text>
|
||||
<text x="20" y="150" class="b">② 核心线程已满</text>
|
||||
<text x="20" y="168" class="sub">放入队列等待</text>
|
||||
<line x1="230" y1="154" x2="262" y2="154" class="arrow "/>
|
||||
<rect x="268" y="136" width="200" height="40" rx="6" class="shadow"/>
|
||||
<rect x="266" y="134" width="200" height="40" rx="6" fill="#E6E6E6"/>
|
||||
<text x="278" y="159" class="b">队列(容量 2)</text>
|
||||
<rect x="478" y="141" width="64" height="26" rx="4" fill="#FFFFFF" stroke="#C8C8C8"/>
|
||||
<text x="510" y="158.5" class="c sub">任务 3</text>
|
||||
<rect x="550" y="141" width="64" height="26" rx="4" fill="#FFFFFF" stroke="#C8C8C8"/>
|
||||
<text x="582" y="158.5" class="c sub">任务 4</text>
|
||||
<text x="20" y="206" class="b">③ 队列已满,线程数小于 4</text>
|
||||
<text x="20" y="224" class="sub">新建非核心线程执行</text>
|
||||
<line x1="230" y1="210" x2="262" y2="210" class="arrow "/>
|
||||
<rect x="268" y="192" width="200" height="40" rx="6" class="shadow"/>
|
||||
<rect x="266" y="190" width="200" height="40" rx="6" fill="#B1DDF0"/>
|
||||
<text x="278" y="215" class="b">非核心线程</text>
|
||||
<rect x="478" y="197" width="64" height="26" rx="4" fill="#FFFFFF" stroke="#C8C8C8"/>
|
||||
<text x="510" y="214.5" class="c sub">任务 5</text>
|
||||
<rect x="550" y="197" width="64" height="26" rx="4" fill="#FFFFFF" stroke="#C8C8C8"/>
|
||||
<text x="582" y="214.5" class="c sub">任务 6</text>
|
||||
<text x="20" y="262" class="b">④ 线程数已达 4 且队列已满</text>
|
||||
<text x="20" y="280" class="sub">执行拒绝策略</text>
|
||||
<line x1="230" y1="266" x2="262" y2="266" class="arrow "/>
|
||||
<rect x="268" y="248" width="200" height="40" rx="6" class="shadow"/>
|
||||
<rect x="266" y="246" width="200" height="40" rx="6" fill="#F3C6C2"/>
|
||||
<text x="278" y="271" class="b">拒绝</text>
|
||||
<rect x="478" y="253" width="64" height="26" rx="4" fill="#FFFFFF" stroke="#C8C8C8"/>
|
||||
<text x="510" y="270.5" class="c sub">任务 7</text>
|
||||
<line x1="20" y1="306" x2="680" y2="306" stroke="#DDD"/>
|
||||
<text x="20" y="328" class="sub">实际执行顺序:0 秒时任务 1、2、5、6 开始执行,1 秒后才轮到队列中的任务 3、4。</text>
|
||||
<text x="20" y="348" class="note">队列满后新建的线程直接执行当前提交的任务,所以后提交的任务 5、6 反而先执行。</text>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 4.7 KiB |
Reference in New Issue
Block a user