Thread pool
medium 70 minA few threads that live long, one queue with a limit, and a loop: take, run, repeat.
Watch lesson5:06
Check yourself
One question at a time. Each answer explains why.
Question 1 of 14
What is the main benefit of a thread pool?
All workers of a fixed pool are busy and the queue has room. A new task
What is the blocking queue for?
Which Java pool creates threads on demand and ends idle ones after 60 seconds?
Which work should not go into a shared pool?
What does `shutdown()` do in Java?
Core 2, maximum 10, unbounded queue, 50 tasks at once. How many threads run?
8 cores, each task waits 90 ms and computes 10 ms. The formula says
Two workers run two tasks, each submits a child to the same pool and waits. What happens?
A worker calls `job.run()` while it holds the lock of the queue. What is the effect?
In work stealing the thief takes the
User B sees the name of user A in a log line. Both requests ran on worker 3. Likely cause?
40 workers, 0.2 s per task, callers wait at most 0.5 s. How long should the queue be?
`future.get(5 seconds)` throws a timeout. The task
Pick an answer to see why.
Not quite. The work is the same. Starting threads is saved and the number is under control.
Not quite. It waits until a worker is free.
Not quite. And it puts idle workers to sleep.
Not quite. The cached pool hands tasks over directly and has no limit on threads.
Not quite. It keeps a worker for good. A few of them starve the pool.
Not quite. `shutdownNow` is the urgent version.
Not quite. The queue is never full, so no extra worker is started.
Not quite. 8 x (1 + 90/10) = 80. Then compare it with connections.
Not quite. Both workers wait, and the children have no worker.
Not quite. Others cannot take a job until it ends.
Not quite. It is far from the owner's end and often the biggest piece.
Not quite. The thread lives on, and so does what sticks to it.
Not quite. 200 tasks per second x 0.5 s = 100.
Not quite. Only the waiting ended. Cancel it if nobody needs it.
Flashcards
Say your answer, then reveal it.
Card 1 of 15
The worker loop?
Wait while empty and open, leave if empty and closed, take one, run it outside the lock.
Say your answer, then reveal it.
Two reasons for a pool?
Reuse of threads, and a limit on how much runs.
Say your answer, then reveal it.
What does submit return?
A future, at once.
Say your answer, then reveal it.
Pool size for computing?
About cores + 1.
Say your answer, then reveal it.
Pool size for waiting?
Cores x (1 + W/C), cut to the scarcest resource.
Say your answer, then reveal it.
Java order for a new task?
Core worker, queue, extra worker, rejection.
Say your answer, then reveal it.
Why is the maximum useless with an unbounded queue?
The queue never says full, so no extra worker starts.
Say your answer, then reveal it.
Which Java pool ends idle threads after 60 s?
The cached pool.
Say your answer, then reveal it.
Caller runs: what is it good for?
Backpressure: the source slows down while it works.
Say your answer, then reveal it.
How does shutdown reach a sleeping worker?
Wake all, interrupt, or one poison pill each.
Say your answer, then reveal it.
How does a pool deadlock without locks?
All workers wait for tasks that sit in its queue.
Say your answer, then reveal it.
Work stealing: which ends?
Owner newest, thief oldest.
Say your answer, then reveal it.
What sticks to a pooled thread?
Thread-locals, interrupt flag, open transactions, held locks.
Say your answer, then reveal it.
Should virtual threads be pooled?
No. One per task, with a semaphore for the limit.
Say your answer, then reveal it.
A timeout on get: does the task stop?
No, only the waiting.
Say your answer, then reveal it.
These cards come back in Today, just before you would forget them.