Collections.synchronized*()
method. This is the most general and basic method, which creates a wrapper that synchronizes all operations using synchronized.If the collection is primarily used for reading, a better performance alternative is
CopyOnWriteArrayList
and the related CopyOnWriteArraySet
. Thread safety is achieved by copying the internal array whenever a modification is made, while the original array remains immutable. Program order is maintained by marking the internal array as volatile
.The third option involves using concurrent collections:
- Non-blocking hash tables such as
ConcurrentSkipListMap
,ConcurrentHashMap
andConcurrentSkipListSet
(which is based on a hash table under the hood). - Non-blocking queues like
ConcurrentLinkedQueue
andConcurrentLinkedDeque
. - A broad range of different blocking queues.