Skip to content
Ameen Abozaid
Planned lessonMID-003-L04

Thread pools: size, queue, rejection

  • Lesson 4 of 7
  • Mid-level
On This Lesson 4 topics

This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.

In this lesson

size a pool and predict its queueing behavior.

Planned topics

  • ThreadPoolExecutor: core size, max size, queue, rejection policy.
  • Order of growth: use core threads, then enqueue, and only add threads up to max when the queue is full. An unbounded queue means max is never used.
  • CPU-bound: about the number of cores. I/O-bound: more, roughly cores x (1 + wait/compute); measure.
  • Use a bounded queue plus an explicit rejection policy (e.g., caller-runs, or abort and report) to create backpressure.
  • newCachedThreadPool has unbounded threads; newFixedThreadPool has an unbounded queue. Both can hurt under load.
Draft outline details

Demonstration

java
new ThreadPoolExecutor(8, 8, 0, TimeUnit.SECONDS,
    new ArrayBlockingQueue<>(100), new ThreadPoolExecutor.CallerRunsPolicy());

Check your understanding

With core 8, max 32, and an unbounded queue, how many threads run?

Show answer

8; the queue never fills.

Practice

Apply this lesson with the module's lab, exercises and quiz.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Continue to next lesson

Type to search levels, phases, modules and lessons.