Thread pool 00:00:00 Make a prediction You will trace a bounded worker pool at saturation and explain which arrival observes the overload policy. There are two workers and two waiting slots. Four long jobs have arrived. Where does the fifth job go? Two jobs are executing and two are waiting. The fifth must follow the admission policy: reject, wait for capacity, or possibly run on the caller if that policy was chosen. There is no invisible fifth slot. 00:00:28 Three shearers and a pen At a small repair shop two mechanics work while two chairs hold waiting customers. Adding more names to a notebook cannot speed the mechanics. The fifth visitor needs an honest answer about waiting or returning another day. Pool size controls simultaneous work; queue capacity bounds accepted waiting. Monitor both so overloaded clients get an intentional outcome. A thread pool is a group of worker threads that live for a long time and run many tasks, one after another. Tasks wait in a queue. Whoever has work submits it and gets a future for the result. Submission and execution are separate: the caller says what to do, the pool decides when and on which thread. To reuse threads. Creating one costs time and memory, and for short tasks this can be more than the task itself. 00:01:22 Choose what the fifth request sees A pool has two workers and two queue slots. Jobs one and two run, while jobs three and four wait. A fifth arrival finds both the workers and the queue full. Choose a documented response such as rejection or bounded waiting. An unbounded queue merely hides overload as rising latency. As the first worker completes, it takes one queued job. Test that an exception in a job is reported and does not quietly eliminate the worker. Submit four jobs while the first two remain busy. Two run and two wait. Now submit the fifth. Choose its contract explicitly: block this submitter, reject immediately, or apply bounded backpressure. Adding an unbounded queue would turn the clear fifth-job result into unpredictable waiting time. 00:02:11 Avoid waiting on the same saturated pool Both workers submit child jobs to their own pool and block until the children finish. The queued children cannot start because the workers are waiting. Compose asynchronous completions, run child work inline where appropriate, or use a separate execution policy. Each running parent submits a child and calls a blocking join. Both workers are now occupied by parents and neither child can start. Adding one worker might rescue this particular run, but nesting can re-create the problem. Use continuations or a deliberately separate execution resource. If both workers submit children to the same saturated pool and wait, the children cannot start even though no mutex cycle exists. Use asynchronous composition or perform suitable child work inline when waiting on the same constrained pool would deadlock. Java ThreadPoolExecutor. Core and maximum size, any blocking queue, four ready rejection policies, hooks before and after each task. Core threads start when tasks arrive, unless they are prestarted. Java ForkJoinPool. Work stealing. A process-wide common pool serves tasks that are not given to another pool. Its threads are daemons. 00:03:30 Try an interview follow-up Why are thread-locals dangerous in a pool? The thread outlives the request. A value left behind is seen by the next task, perhaps of another user. Set it in a wrapper and clear it in a finally. Copy the context at submit, because the worker has another one. A pooled worker finishes a request for user A, then executes an unrelated request for user B. How could a perfectly sized pool still produce an incorrect audit record? A thread-local user identifier can survive task A. Capture B's context when the task is submitted, install it for execution and clear it in cleanup. Test the two jobs on one worker so that the stale context cannot hide behind scheduling. 00:04:21 What to take with you A thread pool runs many tasks on a few threads that stay alive. A caller submits a task, gets a future, and goes on. Each worker sleeps on the queue, takes a task, runs it outside the lock, stores the result or the error, and takes the next. For tasks that mostly compute, start near the number of available cores and measure. For tasks that wait, one starting estimate is the core count times one plus the ratio of time waiting to time computing. Ten milliseconds waiting and five computing gives a multiplier of three. Cap concurrency at the downstream resource limit and check latency under load. Use the arrival rate and tolerated waiting time to estimate a queue bound.