Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Collections

Concurrent collections in Java

Overview of java.util.concurrent collections: ConcurrentHashMap, CopyOnWriteArrayList, ConcurrentLinkedQueue and blocking queues, and when to use each.

The classic collections (ArrayList, HashMap, HashSet) are not thread-safe. If two threads modify one at the same time, you can lose updates, corrupt internal structure or see a ConcurrentModificationException. The java.util.concurrent package provides collections designed for shared use, with much better scalability than wrapping everything in synchronized.

They fall into three families: concurrent maps and sets built on fine-grained locking or compare-and-swap, copy-on-write collections that favor readers, and blocking queues that let threads wait for work. The choice depends on your read/write ratio and whether threads need to wait.

Try it

Java Counting from several threads

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Snapshots and blocking queues

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Which one to use

Need Class Behavior
Shared map ConcurrentHashMap Lock-free reads, atomic merge, compute, putIfAbsent.
Shared sorted map or set ConcurrentSkipListMap, ConcurrentSkipListSet Skip list, O(log n), sorted.
Shared set ConcurrentHashMap.newKeySet() A Set view over a concurrent map.
Mostly-read list or listeners CopyOnWriteArrayList, CopyOnWriteArraySet Every write copies the array; iterators use a snapshot.
Unbounded non-blocking queue ConcurrentLinkedQueue, ConcurrentLinkedDeque Lock-free; size() is O(n).
Producer/consumer with a limit ArrayBlockingQueue, LinkedBlockingQueue put and take block.
Priority work PriorityBlockingQueue, DelayQueue Ordered or time-delayed hand-off.
Hand-off SynchronousQueue, LinkedTransferQueue No or minimal capacity.

Usage notes

  • Thread-safe does not mean compound actions are atomic. if (!map.containsKey(k)) map.put(k, v) is a race even on a ConcurrentHashMap. Use putIfAbsent, computeIfAbsent or merge.
  • Weakly consistent iterators: they never throw ConcurrentModificationException, may or may not show updates made after creation, and never show an element twice.
  • No nulls in ConcurrentHashMap, ConcurrentSkipListMap and most blocking queues, because null would be ambiguous for "absent".
  • Collections.synchronizedList/Map wrap every call in one lock and still need manual locking while iterating. They are fine for light use but do not scale.
  • size() is approximate on concurrent collections that are being modified.
  • Visibility: actions in a thread before putting an element into a concurrent collection happen-before another thread reads it from there.
  • Bound your queues. An unbounded queue lets a fast producer run the JVM out of memory; ArrayBlockingQueue gives natural back-pressure.
  • Copy-on-write is for small, rarely changed data, such as listener lists. Frequent writes cost O(n) each.