Using concurrent collections
In this recipe, you will learn about the thread-safe collections of the java.util.concurrent
package.
Getting ready
A collection can be synchronized if you apply one of the Collections.synchronizeXYZ()
methods to it; here, we have used XYZ as a placeholder that represents either Set
, List
, Map
, or one of the several collection types (see the API of the Collections
class). We have already mentioned that the synchronization applies to the collection itself, not to its iterator or the collection members.
Such synchronized collections are also called wrappers because all of the functionality is still provided by the collections passed as parameters to the Collections.synchronizeXYZ()
methods, while the wrappers provide only thread-safe access to them. The same effect could be achieved by acquiring a lock on the original collection. Obviously, such a synchronization incurs a performance overhead in a multithreading environment, causing each thread to wait for its turn...