Thread pool

스레드 풀

Dispatch jobs to a bounded set of workers and queue excess work.

···
html
<div class="v"><header><b>THREAD POOL</b><span id="state"></span></header><main><div class="queue" id="queue"></div><div class="workers" id="workers"></div></main><footer>bounded workers, queued jobs</footer></div>
css
.v{width:min(92vw,660px);height:min(86vh,310px);box-sizing:border-box;padding:clamp(9px,2.5vmin,18px);border:1px solid var(--line);border-radius:14px;background:var(--surface);display:flex;flex-direction:column;gap:7px;color:var(--fg);font:500 clamp(15px,3.2vmin,17px)/1.25 var(--font-sans, sans-serif)}.v header,.v footer{display:flex;justify-content:space-between;align-items:center;gap:8px;white-space:nowrap}.v header b{color:var(--accent);font-size:.9em}.v header span,.v footer{color:var(--muted);font-size:.82em}.v main{flex:1;min-height:0;position:relative;overflow:hidden}.v .mono{font-family:ui-monospace,SFMono-Regular,Consolas,monospace}.v .active{background:var(--accent)!important;color:var(--bg)!important;border-color:var(--accent)!important}.v .muted{opacity:.28} .queue{height:38%;display:flex;align-items:center;justify-content:center;gap:4px}.queue b{padding:5px;border:1px solid var(--line);border-radius:5px;font:600 .85em ui-monospace,monospace}.workers{height:55%;display:flex;justify-content:center;gap:7px}.worker{width:42%;border:1px solid var(--line);border-radius:6px;display:grid;place-items:center;text-align:center;font:600 .8em ui-monospace,monospace}
js
let n=0;const jobs=["A","B","C","D"];function draw(){document.getElementById("queue").innerHTML=jobs.slice(n+2).concat(jobs.slice(0,Math.max(0,n-2))).map(x=>"<b>"+x+"</b>").join("");document.getElementById("workers").innerHTML=[0,1].map(i=>"<div class=\"worker active\">WORKER "+(i+1)+"<br>job "+jobs[(n+i)%4]+"</div>").join("");document.getElementById("state").textContent="2 active";n=(n+1)%4}draw();setInterval(draw,950)

A thread pool reuses a fixed number of workers. It assigns incoming jobs to idle workers and queues jobs when every worker is busy.

Bounded workers limit resource use, but a growing queue increases latency. CPU-bound and I/O-heavy jobs may need different pool sizes.

When to use

Use it for repeated tasks when creation cost and concurrency limits matter.

Open as page ↗