Concurrent collections
On this page 8 topics
Overview
Topics
- ConcurrentHashMap
- blocking queues
- copy-on-write structures
Learn first
2.5 h of practice (estimate)
Learning outcomes
- Use
ConcurrentHashMapatomic operations (compute,merge,putIfAbsent) correctly. - Build producer-consumer pipelines with
BlockingQueue. - Explain when copy-on-write collections fit and when they hurt.
Lessons
Lab and exercises
Lab
MID-005-LAB1 · Order event buffer
Outcomes: 1–3 (mainly 2) · Time: ~2.5 h Buffer Shop order events in a bounded queue consumed by worker threads. Count events per product with a ConcurrentHashMap. Shut down cleanly with poison pills. Acceptance: producer slows when the queue is full; no events lost (produced equals consumed).
Exercises (assessment: 4 exercises)
| ID | Outcome | Task |
|---|---|---|
| MID-005-EX01 | 1 | Count words from 4 threads using HashMap and get/put; show lost updates; fix with merge. |
| MID-005-EX02 | 1 | Rewrite a containsKey/put cache fill with computeIfAbsent; explain what to avoid inside the function. |
| MID-005-EX03 | 2 | Build a producer-consumer with ArrayBlockingQueue(10); show blocking put and a clean poison-pill stop. |
| MID-005-EX04 | 3 | For three cases (listener list, 500k-entry list written often, small read-mostly config), say whether copy-on-write fits and why. |
Show solutions
- EX01 The
HashMaptotal is below the expected count.map.merge(word, 1, Integer::sum)on aConcurrentHashMapgives the exact total. - EX02
cache.computeIfAbsent(k, this::load). Keep the function short, do not block on slow I/O if avoidable, and never modify the same map inside it. - EX03 The producer blocks when 10 items wait. Stop by putting one poison object per consumer; each consumer exits on receiving it.
- EX04 Listener list: yes (read-mostly, small). 500k list written often: no (copy per write). Small read-mostly config: yes.
- The lab's backpressure criterion also covers outcome 2 in practice.
Quiz
Outcomes: O1 ConcurrentHashMap · O2 blocking queues · O3 copy-on-write.
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.
Practical part
Four exercises MID-005-EX01 to EX04 plus the lab. Pass: at least three exercises correct and the lab showing backpressure with no lost events.
Common mistakes
get then put on a concurrent map; unbounded queues.
Assessment
4 exercises.
Career relevance
Backpressure in messaging (SRB-006).