AlgoViz

Thread pool

medium 70 min

A few threads that live long, one queue with a limit, and a loop: take, run, repeat.

Watch lesson5:06

When it comes up

The moments this idea belongs in a design interview, and the moments it does not.

Words that announce it

You hearThinkFirst sentence
"Process in the background"Pool with a futureDoes the caller need the result?
"Many small tasks"Pool, maybe stealingHow small, and do they split?
"A burst after lunch"Queue length and policyHow long may one wait, and may I refuse?
"Design a job scheduler"A pool is inside itI separate when from how
"Implement ExecutorService"This pageLoop, queue, submit, shutdown, in this order
"It hangs under load"Pool that waits for itselfDo tasks wait for tasks of the same pool?

The interviewer says: implement a thread pool. You have 25 minutes.

BreaksStart with the options
How
Core, maximum, keep alive, priorities.
Limit
After 25 minutes nothing runs.
Choose when
Never.
WorksWrap the library
How
A class around ThreadPoolExecutor.
Limit
Shows usage and not understanding.
Choose when
When the pool is a part of a larger problem.
StrongThe loop first
How
Job, worker loop, submit, shutdown in 40 lines. Then a trace, then the limit, the policy and the starving pool as talk.
Limit
Know the condition variable by heart.
Choose when
Here.

Twenty-five minutes

  1. 1

    3: questions

    Result or not, limit, refusal, order, closing.

  2. 2

    4: classes

    Pool, job, future from the library. The table of rules for the lock.

  3. 3

    10: code

    Job, loop, submit, shutdown.

  4. 4

    4: trace

    Six tasks, three workers, then a failing task, then closing.

  5. 5

    4: follow-ups

    Overload, sizing, waiting for itself.