Skip to content
Ameen Abozaid
MID-003

Java concurrency

  • 7 Lessons
  • 4 h practice (estimate)
On this page 8 topics

Overview

Topics

  • Threads
  • Runnable
  • Callable
  • ExecutorService
  • thread pools

Learn first

4 h of practice (estimate)

Learning outcomes

  1. Create and coordinate threads and explain thread states.
  2. Submit Runnable and Callable tasks to an ExecutorService and collect Future results.
  3. Size a thread pool for CPU-bound vs I/O-bound work and explain queueing behavior.
  4. Shut executors down correctly and handle task failures.

Lessons

  1. 01Why concurrency: throughput and latency
  2. 02Threads and their lifecycle
  3. 03Executors and futures
  4. 04Thread pools: size, queue, rejection
  5. 05Shutdown and failure handling
  6. 06Demo: parallelizing the price-feed import
  7. 07Demo: a thread pool that silently drops work

Lab and exercises

Lab

MID-003-LAB1 · Parallel import

Outcomes: 1–4 · Time: ~4 h Import product prices from several simulated suppliers (varying latency, one failing, one timing out) concurrently with a bounded pool, per-task timeouts, and a failure report. Shut the pool down correctly. Compare wall-clock time with the sequential version. Acceptance: wall-clock improvement measured; no lost tasks (imported + failed + timed out equals submitted).

Exercises (assessment: pool-sizing scenarios plus the lab)

IDOutcomeTask
MID-003-EX011Print a thread's state at NEW, after start, while in sleep, while blocked on a monitor, and after finish.
MID-003-EX022Submit three Callable<Integer> tasks, one throwing; collect results and show how the exception appears.
MID-003-EX033Size a pool for 8 cores: (a) image resizing, (b) supplier HTTP calls waiting 400 ms with 100 ms compute.
MID-003-EX043Predict how many threads run for core 4, max 16, LinkedBlockingQueue unbounded and 100 tasks; then for ArrayBlockingQueue(10).
MID-003-EX054Write a shutdown helper using shutdown, awaitTermination, shutdownNow.
MID-003-EX064Make a pool reject work visibly when full and count rejections.
Show solutions
  • EX01 getState(): NEW, RUNNABLE, TIMED_WAITING, BLOCKED, TERMINATED.
  • EX02 Future.get() throws ExecutionException; getCause() is the task's exception; the other results still arrive.
  • EX03 (a) about 8 threads. (b) cores x (1 + 400/100) = 8 x 5 = 40 threads as a starting point; then measure.
  • EX04 Unbounded queue: 4 threads, 96 queued. Bounded queue of 10: 4 core threads + 10 queued, then up to 12 extra threads (max 16) for the next 12 tasks, remaining tasks rejected: 100 - 26 = 74 rejected under the default abort policy if all submitted at once.
  • EX05 pool.shutdown(); if (!pool.awaitTermination(t, SECONDS)) pool.shutdownNow(); with interrupt handling around the wait.
  • EX06 ThreadPoolExecutor with a bounded queue and a custom RejectedExecutionHandler that increments an AtomicInteger and throws or runs on the caller.
  • Outcome 1's coordination is also in the lab; the L02 join/interrupt points are checked in Q02 and Q03.

Quiz

Outcomes: O1 threads and states · O2 executors and futures · O3 pool sizing · O4 shutdown and failures.

A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.

  1. 1To run code on a new thread you call:O1
  2. 2A thread waiting to enter a synchronized block is in state:O1
  3. 3join() on a thread:O1
  4. 4Callable differs from Runnable by:O2
  5. 5A task's exception reaches the caller of Future.get() as:O2
  6. 6A good starting size for a CPU-bound pool is about:O3
  7. 7With an unbounded queue and core size 4, max 16, extra threads beyond 4 are:O3
  8. 8Which factory has an unbounded thread count?O3
  9. 9Correct shutdown order is:O4
  10. 10Which policy silently loses tasks?O4

Practical part

Six exercises MID-003-EX01 to EX06 (including two pool-sizing scenarios) plus the lab. Pass: at least five exercises correct and the lab showing a measured speed-up with no lost tasks.

Common mistakes

Unbounded queues; newCachedThreadPool under load; never calling shutdown.

Assessment

Pool-sizing scenarios plus lab.

Career relevance

Tomcat and connection-pool sizing (MID-017).

Type to search levels, phases, modules and lessons.