Java Concurrent Collections
Java concurrent collections are thread-safe lists, sets, maps and queues from the java.util.concurrent package. Several threads can read and change them at the same time without corrupting data and without you writing synchronized blocks.
Regular collections such as ArrayList and HashMap are fast but not thread-safe. This lesson shows what goes wrong with them under concurrency and how ConcurrentHashMap, CopyOnWriteArrayList, ConcurrentLinkedQueue, BlockingQueue and ConcurrentSkipListMap solve the problem.
Why Regular Collections Fail with Multiple Threads
ArrayList.add() reads the current size, stores the element, and then increases the size. When two threads do this at the same moment, one write can overwrite the other, the same kind of race condition you saw with counter++ in Java Threads.
import java.util.ArrayList;
import java.util.List;
public class UnsafeListDemo {
public static void main(String[] args) throws InterruptedException {
List<Integer> numbers = new ArrayList<>(); // NOT thread-safe
Runnable addTenThousand = () -> {
for (int i = 0; i < 10_000; i++) {
numbers.add(i);
}
};
Thread first = new Thread(addTenThousand);
Thread second = new Thread(addTenThousand);
first.start();
second.start();
first.join();
second.join();
System.out.println("Expected size: 20000");
System.out.println("Actual size : " + numbers.size());
}
}
Output from one run (it changes every time, and the program can also crash with ArrayIndexOutOfBoundsException):
Expected size: 20000
Actual size : 13587
How it works:
- Both threads call
add()on the sameArrayListat the same time. - Their internal steps interleave, so some elements overwrite each other and are lost.
- If one thread grows the internal array while the other writes to it,
add()can even throw an exception.
Note: A bug like this may not show up on your laptop and then appear in production under heavy load. Never share a plain
ArrayList,HashMaporHashSetbetween threads that modify it.
Synchronized Wrappers vs Concurrent Collections
Java offers two ways to make collections thread-safe:
| Synchronized wrapper | Concurrent collection | |
|---|---|---|
| Example | Collections.synchronizedList(new ArrayList<>()) | CopyOnWriteArrayList, ConcurrentHashMap |
| Locking | One lock for the whole collection | Fine-grained locks, CAS operations or copy-on-write |
| Many threads at once | Threads wait for each other | Threads mostly work in parallel |
| Iterating while others modify | Must lock manually, otherwise ConcurrentModificationException | Safe, never throws ConcurrentModificationException |
| Atomic compound actions | Not available | putIfAbsent, merge, compute, ... |
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class SynchronizedWrapperDemo {
public static void main(String[] args) throws InterruptedException {
List<Integer> numbers = Collections.synchronizedList(new ArrayList<>());
Runnable addTenThousand = () -> {
for (int i = 0; i < 10_000; i++) {
numbers.add(i);
}
};
Thread first = new Thread(addTenThousand);
Thread second = new Thread(addTenThousand);
first.start();
second.start();
first.join();
second.join();
System.out.println("Size: " + numbers.size());
// Iteration is NOT protected automatically: lock the list yourself
long evenCount;
synchronized (numbers) {
evenCount = numbers.stream().filter(n -> n % 2 == 0).count();
}
System.out.println("Even numbers: " + evenCount);
}
}
Output:
Size: 20000
Even numbers: 10000
How it works:
- Every method of the wrapper locks the list, so
add()calls no longer lose elements. - Iterating (including streams) is several calls in a row, so the wrapper cannot protect it. The
synchronized (numbers)block does that job. - Concurrent collections remove both problems: they scale better and are safe to iterate without extra locks.
ConcurrentHashMap in Java
ConcurrentHashMap is the thread-safe replacement for HashMap. Reads never block, and writes lock only a small part of the map, so many threads can use it at once. It also offers atomic compound methods that read and update a value as one step.
| Method | What it does atomically |
|---|---|
putIfAbsent(key, value) | Adds the entry only if the key is missing |
computeIfAbsent(key, fn) | Creates the value with fn only if the key is missing |
compute(key, fn) | Replaces the value with fn(key, oldValue) |
merge(key, value, fn) | Inserts value, or combines it with the old value using fn |
getOrDefault(key, default) | Reads a value with a fallback |
Counting Words with merge()
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class WordCountDemo {
public static void main(String[] args) {
List<String> reviews = List.of(
"java is fast", "java is fun", "threads are fast",
"java threads are fun", "fun fun fun");
Map<String, Integer> wordCounts = new ConcurrentHashMap<>();
try (ExecutorService executor = Executors.newFixedThreadPool(4)) {
for (String review : reviews) {
executor.submit(() -> {
for (String word : review.split(" ")) {
wordCounts.merge(word, 1, Integer::sum); // atomic "add one"
}
});
}
} // close() waits for every task
System.out.println(new TreeMap<>(wordCounts)); // sorted for readable output
}
}
Output:
{are=2, fast=2, fun=5, is=2, java=3, threads=2}
How it works:
- Four pool threads process the reviews at the same time and update the same map.
merge(word, 1, Integer::sum)puts1for a new word, or adds1to the old count, as a single atomic step. No update is lost.- Copying into a
TreeMaponly sorts the keys for printing.
Note:
map.put(word, map.get(word) + 1)is not safe even on aConcurrentHashMap, because another thread can change the value betweenget()andput(). Usemerge()orcompute()instead.
computeIfAbsent, compute and Null Values
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class ConcurrentHashMapMethods {
public static void main(String[] args) {
Map<String, List<String>> studentsByCourse = new ConcurrentHashMap<>();
// computeIfAbsent creates the list only the first time
studentsByCourse.computeIfAbsent("Java", course -> new ArrayList<>()).add("Asha");
studentsByCourse.computeIfAbsent("Java", course -> new ArrayList<>()).add("Ravi");
studentsByCourse.computeIfAbsent("Python", course -> new ArrayList<>()).add("Meena");
System.out.println(studentsByCourse);
Map<String, Integer> seats = new ConcurrentHashMap<>(Map.of("Java", 30, "Python", 25));
// compute: take one seat, but never go below zero
seats.compute("Java", (course, left) -> left > 0 ? left - 1 : 0);
System.out.println("Java seats left: " + seats.get("Java"));
System.out.println("Go seats: " + seats.getOrDefault("Go", 0));
try {
seats.put("Rust", null);
} catch (NullPointerException e) {
System.out.println("ConcurrentHashMap does not allow null keys or values");
}
}
}
Output (the order of map entries can differ):
{Java=[Asha, Ravi], Python=[Meena]}
Java seats left: 29
Go seats: 0
ConcurrentHashMap does not allow null keys or values
How it works:
computeIfAbsent()creates theArrayListfor "Java" once. The second call finds it and returns the existing list.compute()reads the old value and writes the new one in one atomic step.ConcurrentHashMaprejectsnullkeys and values, becauseget()returningnullmust clearly mean "no such key".
Note: The lists stored inside the map are plain
ArrayLists. If several threads add students to the same course, make the inner lists thread-safe too, for example withCopyOnWriteArrayList.
CopyOnWriteArrayList in Java
CopyOnWriteArrayList makes a fresh copy of its internal array on every write. Readers always work on a stable snapshot, so iterating is fast, needs no lock, and never throws ConcurrentModificationException. The trade-off is that writes are expensive, so use it for lists that are read often and changed rarely, such as listener lists or configuration.
import java.util.ArrayList;
import java.util.ConcurrentModificationException;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
public class CopyOnWriteDemo {
public static void main(String[] args) {
// 1. A normal ArrayList fails if it is modified during iteration
List<String> plainList = new ArrayList<>(List.of("email", "sms"));
try {
for (String channel : plainList) {
if (channel.equals("email")) {
plainList.add("push");
}
}
} catch (ConcurrentModificationException e) {
System.out.println("ArrayList: ConcurrentModificationException");
}
// 2. CopyOnWriteArrayList iterates over a snapshot
List<String> safeList = new CopyOnWriteArrayList<>(List.of("email", "sms"));
for (String channel : safeList) {
System.out.println("notifying via " + channel);
if (channel.equals("email")) {
safeList.add("push"); // goes into a new copy, not the one being iterated
}
}
System.out.println("List after loop: " + safeList);
}
}
Output:
ArrayList: ConcurrentModificationException
notifying via email
notifying via sms
List after loop: [email, sms, push]
How it works:
- Changing an
ArrayListwhile a for-each loop walks over it makes the iterator fail. - The
CopyOnWriteArrayListiterator walks the snapshot taken when the loop started, so "push" is not visited in this loop. - After the loop, the list contains all three channels.
CopyOnWriteArraySetoffers the same behaviour for sets.
ConcurrentLinkedQueue: A Non-Blocking Queue
ConcurrentLinkedQueue is an unbounded, thread-safe FIFO queue that uses lock-free compare-and-swap operations. offer() adds at the tail, and poll() removes from the head or returns null immediately if the queue is empty. It never makes a thread wait.
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
public class ConcurrentQueueDemo {
public static void main(String[] args) {
Queue<String> printJobs = new ConcurrentLinkedQueue<>();
for (int job = 1; job <= 9; job++) {
printJobs.offer("job-" + job);
}
AtomicInteger printed = new AtomicInteger();
try (ExecutorService printers = Executors.newFixedThreadPool(3)) {
for (int p = 0; p < 3; p++) {
printers.submit(() -> {
String job;
while ((job = printJobs.poll()) != null) { // null means: queue empty
printed.incrementAndGet();
}
});
}
}
System.out.println("Jobs printed : " + printed.get());
System.out.println("Queue empty? : " + printJobs.isEmpty());
}
}
Output:
Jobs printed : 9
Queue empty? : true
How it works:
- Three printer threads take jobs from the same queue at the same time.
poll()is atomic, so each job is taken by exactly one printer; none is printed twice or lost.- When the queue is empty,
poll()returnsnulland each printer loop ends. The counter is anAtomicInteger, covered in Java Atomic Classes.
BlockingQueue: Producer-Consumer in Java
A BlockingQueue adds waiting operations. put() waits while a bounded queue is full, and take() waits while the queue is empty. This makes it the standard tool for the producer-consumer pattern, where some threads create work and others process it.
| Operation | Throws exception | Returns special value | Blocks | Times out |
|---|---|---|---|---|
| Insert | add(e) | offer(e) → false | put(e) | offer(e, time, unit) |
| Remove | remove() | poll() → null | take() | poll(time, unit) |
| Examine | element() | peek() → null | – | – |
Common implementations are ArrayBlockingQueue (bounded, array-based), LinkedBlockingQueue (optionally bounded), PriorityBlockingQueue (ordered by priority) and DelayQueue (elements become available after a delay).
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class ProducerConsumerDemo {
private static final String POISON_PILL = "DONE"; // tells the consumer to stop
public static void main(String[] args) throws InterruptedException {
BlockingQueue<String> orders = new ArrayBlockingQueue<>(2); // holds at most 2 orders
Thread kitchen = new Thread(() -> {
try {
while (true) {
String order = orders.take(); // waits while the queue is empty
if (order.equals(POISON_PILL)) {
break;
}
Thread.sleep(100); // cooking takes time
System.out.println("Kitchen cooked " + order);
}
System.out.println("Kitchen closed");
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}, "kitchen");
kitchen.start();
String[] menu = {"Dosa", "Idli", "Pongal", "Vada"};
for (String dish : menu) {
orders.put(dish); // waits while the queue is full
System.out.println("Waiter queued " + dish + " (queue size " + orders.size() + ")");
}
orders.put(POISON_PILL);
kitchen.join();
}
}
Output from one run:
Waiter queued Dosa (queue size 1)
Waiter queued Idli (queue size 1)
Waiter queued Pongal (queue size 2)
Kitchen cooked Dosa
Waiter queued Vada (queue size 2)
Kitchen cooked Idli
Kitchen cooked Pongal
Kitchen cooked Vada
Kitchen closed
How it works:
- The waiter (main thread) is the producer; the kitchen thread is the consumer.
- The queue holds only two orders. When it is full,
put()makes the waiter wait until the kitchen takes an order, so a fast producer cannot overwhelm a slow consumer. take()makes the kitchen wait when there is nothing to cook, without wasting CPU in a loop.- The special
"DONE"order, a poison pill, tells the consumer to stop cleanly.
ConcurrentSkipListMap: A Sorted Concurrent Map
ConcurrentSkipListMap is the concurrent version of TreeMap: its keys stay sorted, and it supports navigation methods such as firstKey(), headMap() and descendingMap(). ConcurrentSkipListSet is the matching sorted set.
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class SkipListLeaderboard {
public static void main(String[] args) {
// score -> player, highest score first
ConcurrentSkipListMap<Integer, String> leaderboard =
new ConcurrentSkipListMap<>((a, b) -> Integer.compare(b, a));
String[] players = {"Arun", "Bala", "Chitra", "Divya", "Elan"};
int[] scores = {870, 1240, 990, 1510, 640};
try (ExecutorService gameServers = Executors.newFixedThreadPool(3)) {
for (int i = 0; i < players.length; i++) {
int index = i;
gameServers.submit(() -> leaderboard.put(scores[index], players[index]));
}
}
System.out.println("Leaderboard : " + leaderboard);
System.out.println("Top player : " + leaderboard.firstEntry().getValue());
System.out.println("Score >= 990: " + leaderboard.headMap(990, true).values());
}
}
Output:
Leaderboard : {1510=Divya, 1240=Bala, 990=Chitra, 870=Arun, 640=Elan}
Top player : Divya
Score >= 990: [Divya, Bala, Chitra]
How it works:
- Three game-server threads insert scores at the same time, and the map keeps them sorted.
- The comparator
Integer.compare(b, a)reverses the order, so the highest score comes first. headMap(990, true)returns every entry up to and including 990 in the map's order, which here means all scores of 990 or more.
Choosing Between Java Concurrent Collections
| Need | Use |
|---|---|
| Thread-safe map, many readers and writers | ConcurrentHashMap |
| Sorted thread-safe map or set | ConcurrentSkipListMap, ConcurrentSkipListSet |
| List read often, changed rarely | CopyOnWriteArrayList |
| Set read often, changed rarely | CopyOnWriteArraySet |
| Non-blocking FIFO queue | ConcurrentLinkedQueue, ConcurrentLinkedDeque |
| Producer-consumer with waiting | ArrayBlockingQueue, LinkedBlockingQueue |
| Tasks ordered by priority | PriorityBlockingQueue |
| Thread-safe set backed by a map | ConcurrentHashMap.newKeySet() |
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ConcurrentSetDemo {
public static void main(String[] args) {
Set<String> visitedPages = ConcurrentHashMap.newKeySet();
String[] clicks = {"/home", "/java", "/home", "/python", "/java", "/home"};
try (ExecutorService executor = Executors.newFixedThreadPool(3)) {
for (String page : clicks) {
executor.submit(() -> visitedPages.add(page));
}
}
System.out.println("Unique pages: " + visitedPages.size());
}
}
Output:
Unique pages: 3
How it works:
ConcurrentHashMap.newKeySet()returns aSetbacked by aConcurrentHashMap, soadd()is thread-safe.- Duplicate clicks from different threads are stored only once, leaving three unique pages.
Real-World Example: A Multithreaded Log Analyzer
A web server writes access-log lines. Two reader threads (producers) feed lines into a bounded queue, three analyzer threads (consumers) parse them, and the results go into a shared ConcurrentHashMap. A CopyOnWriteArrayList collects slow requests for an alert report.
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
public class LogAnalyzer {
private static final String END = "<END>";
private static final int ANALYZERS = 3;
public static void main(String[] args) throws InterruptedException {
// Two log files, each line: method path status durationMs
List<String> serverA = List.of(
"GET /home 200 35", "GET /java 200 48", "POST /login 401 12",
"GET /api/courses 500 950", "GET /home 200 30");
List<String> serverB = List.of(
"GET /java 200 52", "GET /missing 404 5", "GET /api/courses 200 1200",
"POST /login 200 80", "GET /home 200 41");
BlockingQueue<String> lines = new LinkedBlockingQueue<>(4); // bounded buffer
Map<String, Integer> statusCounts = new ConcurrentHashMap<>();
Map<String, Integer> hitsPerPath = new ConcurrentHashMap<>();
List<String> slowRequests = new CopyOnWriteArrayList<>();
try (ExecutorService pool = Executors.newFixedThreadPool(5)) {
// Consumers: parse lines until they receive the END marker
for (int a = 0; a < ANALYZERS; a++) {
pool.submit(() -> {
while (true) {
String line = lines.take();
if (line.equals(END)) {
return null;
}
String[] parts = line.split(" ");
String path = parts[1];
String status = parts[2];
int durationMs = Integer.parseInt(parts[3]);
statusCounts.merge(status, 1, Integer::sum);
hitsPerPath.merge(path, 1, Integer::sum);
if (durationMs > 500) {
slowRequests.add(path + " took " + durationMs + " ms");
}
}
});
}
// Producers: read both "files" at the same time
var readerA = pool.submit(() -> { for (String l : serverA) lines.put(l); return null; });
var readerB = pool.submit(() -> { for (String l : serverB) lines.put(l); return null; });
try {
readerA.get();
readerB.get();
} catch (Exception e) {
throw new IllegalStateException("reader failed", e);
}
for (int a = 0; a < ANALYZERS; a++) {
lines.put(END); // one stop signal per consumer
}
}
System.out.println("Requests by status: " + new TreeMap<>(statusCounts));
System.out.println("Hits per path : " + new TreeMap<>(hitsPerPath));
System.out.println("Slow requests : " + slowRequests.stream().sorted().toList());
int total = statusCounts.values().stream().mapToInt(Integer::intValue).sum();
System.out.println("Total lines : " + total);
}
}
Output:
Requests by status: {200=7, 401=1, 404=1, 500=1}
Hits per path : {/api/courses=2, /home=3, /java=2, /login=2, /missing=1}
Slow requests : [/api/courses took 1200 ms, /api/courses took 950 ms]
Total lines : 10
How it works:
- Bounded hand-off.
LinkedBlockingQueue<>(4)holds at most four lines. Readers wait withput()when analyzers fall behind, which keeps memory use under control. - Parallel parsing. Three analyzers call
take()on the same queue. Each line is delivered to exactly one of them. - Lock-free counting.
merge(key, 1, Integer::sum)on twoConcurrentHashMaps updates counters from all three analyzers without lost updates. - Rarely-written list. Only slow requests are added to the
CopyOnWriteArrayList, so the copy-on-write cost stays low while reading it is free. - Clean shutdown. After both readers finish, the main thread puts one
ENDmarker per analyzer, so every consumer stops. Closing the pool then waits for them. - Deterministic report. The threads finish in a random order, but sorting with
TreeMapandsorted()makes the printed report identical on every run.
The analyzers above are Callable tasks (they return null), because take() throws a checked InterruptedException. See Java Callable Interface for why that matters, and Java Atomic Classes for thread-safe single counters.