Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: DSA in Java
Stage 5: Specialization

Part 12 / Topic 5 of 7

java.util.concurrent collections

Section 2 fixed one counter with synchronized. Locking by hand around every read and write of a shared Map or List works but is easy to get wrong and easy to forget on one call site. java.util.concurrent ships collections that are already thread-safe, often faster than "a plain collection plus your own lock" because the locking is done at a much finer grain inside. Think of a shared office supply cabinet with its own built-in queue system, instead of one key that everyone has to wait their turn for.

Collection Thread-safe for Typical use
ConcurrentHashMap many readers and writers on different keys, at once counters, caches keyed by ID
CopyOnWriteArrayList far more reads than writes a list of listeners, rarely-changed config
BlockingQueue (LinkedBlockingQueue) one or more producers, one or more consumers a producer/consumer hand-off, no manual wait/notify
Map<String, Integer> itemCounts = new ConcurrentHashMap<>();
itemCounts.merge(item, 1, Integer::sum);     // atomic "add one, or start at one" -- no lock needed by you

BlockingQueue<String> slips = new LinkedBlockingQueue<>();
slips.put(drink);           // producer: never blocks on a bounded queue with room
String slip = slips.take(); // consumer: blocks here until something arrives
$ javac ConcurrentCollections.java
$ java ConcurrentCollections
== ConcurrentHashMap: safe counts updated from many threads ==
chai: 4, samosa: 2, dosa: 2
total counted matches total orders: true

== CopyOnWriteArrayList: safe to read while another thread writes ==
reader sees: [filter coffee]
reader sees: [filter coffee, masala dosa]
reader sees: [filter coffee, masala dosa]
final specials: [filter coffee, masala dosa]

== BlockingQueue: one producer, two consumers, no explicit lock ==
packed count: 4
all four drinks packed: true

Map.merge(key, 1, Integer::sum) is the key line: it reads-and-writes the count for one key as a single atomic step on ConcurrentHashMap, with no synchronized block anywhere in this code, and four threads splitting up the same order list still land on the exact right totals. CopyOnWriteArrayList's reader thread never sees a half-written list or throws ConcurrentModificationException while another thread appends — every read sees either the list before the write or after it, never a torn state, because a write replaces the whole backing array instead of mutating it in place. The BlockingQueue example runs two consumer threads pulling from one queue with no shared counter, no lock, and no busy-waiting — take() simply blocks until something is there, and a "__STOP__" sentinel value (one per consumer) tells each one when to stop.

⚠️ CopyOnWriteArrayList copies the entire backing array on every write. Great for a list that's read constantly and written rarely (listeners, config); a poor choice for a list with frequent writes — each one is an O(n) copy.

Full program: examples/ConcurrentCollections.java.

Done here? The practice set for Part 12 — including the solutions — is on the part page.

Search the guide