Professional Documents
Culture Documents
Collections - Collections Java
Collections - Collections Java
Fail-fast iterators → when a collection is modified by a thread while another thread iterates over the
collection => ConcurrentModificationException => Synchronized Wrappers = their synchronization
mechanism uses the collection object itself as the lock object; that means when a thread is iterating
over elements in a collection, all other collection’s methods block, causing other threads having to
wait
Obs!
Note that copy-on-write collections have snapshot iterators which do not throw
ConcurrentModificationException. Since these collections are backed by immutable arrays, an
iterator can read the values in one of these arrays (but never modify them) without danger of them
being changed by another thread.
2. Compare-And-Swap
• the collections in this group implement thread safety using an algorithm called Compare-
And-Swap (CAS) which can be understood like this
◦ it compares the contents of a memory location with a given value and, only if they are
the same, modifies the contents of that memory location to a new given value (usage of
AtomicBoolean - http://tutorials.jenkov.com/java-concurrency/compare-and-swap.html)
• collections using CAS include ConcurrentLinkedQueue and ConcurrentSkipListMap.
Obs!
Note that the CAS collections have weakly consistent iterators, which reflect some but not
necessarily all of the changes that have been made to their backing collection since they were
created. Weakly consistent iterators do not throw ConcurrentModificationException.
Collections in this group also have weakly consistent iterators and do not throw
ConcurrentModificationException.
Collections bug:
1.7 – Collections.sort() sorteaza gresit -1 si Integer.MAX_VALUE datorita overflow-ului
Collection vs Collections
Collection is an interface that is the base interface whereas Collections is an utility class .
It consists of only static methods which are used to operate on objects of type Collection (give some
examples of Collections methods)
Collections does not use primitives. They use wrapper classes of the primitive types, but there are
collections that uses primitives type:
• Trove
• FastUtil
• Goldman Sachs (GS) Collections
• Colt
For my knowledge:
OpenJDK is an open source version of Sun JDK. Oracle JDK is Sun's official JDK.
OracleJDK refers Java SE (Standard Edition)