| Start |
|---|
| Thread-safe Collections By default, the various Collection types are not thread-safe. However, it's fairly easy to make a collection thread-safe. When you make a thread-safe collection, you should never access it through the original collection, only through the thread-safe wrapper. Starting in Java 5, |
| Concurrent Collections Concurrent collections are a generalization of thread-safe collections, that allow for a broader usage in a concurrent environment. While thread-safe collections have safe element addition or removal from multiple threads, they do not necessarily have safe iteration in the same context (one may not be able to safely iterate through the collection in one thread, while another one modifies it by adding/removing elements). This is where concurrent collections are used. As iteration is often the base implementation of several bulk methods in collections, like For example, the Java SE 5 The "snapshot" style iterator method uses a reference to the state of the array at the point that the iterator was created. This array never changes during the lifetime of the iterator, so interference is impossible and the iterator is guaranteed not to throw ConcurrentModificationException. Therefore, the following code is safe : Another concurrent collection regarding iteration is Iterators are weakly consistent, returning elements reflecting the state of the queue at some point at or since the creation of the iterator. They do not throw java.util.ConcurrentModificationException, and may proceed concurrently with other operations. Elements contained in the queue since the creation of the iterator will be returned exactly once. One should check the javadocs to see if a collection is concurrent, or not. The attributes of the iterator returned by the Thread safe but non concurrent examplesIn the above code, changing the Could (and statistically will on most modern, multi CPU/core architectures) lead to exceptions. Synchronized collections from the |
Insertion into ConcurrentHashMap |
Read Article
https://develop.horje.com/learn/1434/reference