Concurrent collections

Concurrent collections allows multiple threads to access data independently without blocking each other.

ConcurrentHashMap

A hash table supporting full concurrency of retrievals and high expected concurrency for updates. Implements ConcurrentMap interface.

Bulk operations such as putAll() are not guaranteed to be performed atomically.

Unlike Hashtable, ConcurrentHashMap uses multiple buckets to store data. This avoids read locks and greatly improves performance.

Unlike HashMap, it does not throw ConcurrentModificationException exception while one thread iterating over map and other modifies map.

ConcurrentSkipListMap

A scalable concurrent ConcurrentNavigableMap implementation. The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used.

Unlike ConcurrentHashMap, it guarantees O(log(n)) performance for most of its operations, does not allow to modify the concurrent thread count and is sorted.

ConcurrentSkipListSet

A scalable concurrent NavigableSet implementation based on a ConcurrentSkipListMap. The elements of the set are kept sorted according to their natural ordering, or by a Comparator provided at set creation time, depending on which constructor is used.

Bulk operations such as addAll() or forEach(), are not guaranteed to be performed atomically.

CopyOnWriteArrayList

A thread-safe variant of ArrayList in which all mutative operations (add, set, and so on) are implemented by making a fresh copy of the underlying array. Element-changing operations on iterators themselves (remove, set, and add) are not supported. These methods throw UnsupportedOperationException.

Unlike ArrayList, it does not throw ConcurrentModificationException exception while one thread iterating over list and other modifieslist map.

Unlike Vector, it has a longer delay for write (due to copying) but no delay for reads. And it doesn't require explicit synchronization when iterating, so two threads can write to it at the same time. If you have much more reads than writes, use CopyOnWriteArrayList, otherwise use Vector.

CopyOnWriteArraySet

A Set that uses an internal CopyOnWriteArrayList for all of its operations.

ConcurrentLinkedQueue

An unbounded thread-safe queue based on linked nodes. This queue orders elements FIFO (first-in-first-out).

A ConcurrentLinkedQueue is an appropriate choice when many threads will share access to a common collection.

Unlike a LinkedBlockingQueue, a ConcurrentLinkedQueue does not block a thread once the queue is empty. Instead, it returns null. Since its unbounded, it'll throw a java.lang.OutOfMemoryError if there's no extra memory to add new elements.