You are on page 1of 2

• before JDK 1.

2 there was no Collection framework


• main used classes for data structures were Vector, Hashtable, Stack, Properties and Array

Downsides of old objects:


• course-grain syncronization (sincronizare la nivel de obiect si nu la nivel de operatie)
• poor design: Stack extends Vector

New collections come into place but:


• they are not syncronized (not thread-safe) – beacause it adds preformance bottlenecks for
single-threaded applications → Synchronized Wrappers

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

java.util.concurrent: concurrent collections divided into 3 groups:


1. CopyOnWrite Collections
• this kind of thread-safe collections stores values in an immutable array; any change to the
value of the collection results in a new array being created to reflect the new values
• these collections are designed for situations in which read operations greatly predominate
write operations
• 2 implementations of this kind: CopyOnWriteArrayList and CopyOnWriteArraySet

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.

3. ConcurrentCollections using special lock object


• unlike synchronization code, in which an object lock is held while a code block or a method
is executed, a Lock is held until its unlock() method is called.
• Implementations: BlockingQueue (ArrayBlockingQueue, LinkedBlockingQueue),
ConcurrentHashMap
Obs!

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

Where bugs cand be found/reported:


https://bugs.openjdk.java.net/projects/JDK/issues

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)

You might also like