Concurrent collections in Java

Concurrent collections in Java

This chapter gives a basic overview about the concurrent collections in Java. Most of the  collection elements in the java.util package are not thread safe.The legacy collections such as Vector and Hashtable are conditionally thread safe. Also considerable amount of performance downfall is also there with legacy collections. In Java 1.2  ArrayList and HashMap were introduced. Also their concurrent wrapper classes such as Collections.synchronizedList() and Collections.synchronizedMap() were introduced.Vector and Collections.synchronizedList() are almost equal.Similarly Hashtable and Collections.synchronizedMap() are equal.

CopyOnWriteArrayList and ConcurrentHashMap

1) The legacy classes and concurrent wrappers cannot be considered as  completely thread safe. Because the iteration of legacy classes and  concurrent wrappers is not thread  safe(Other methods are thread safe ). If we iterate Vector and if we do another operation on it then   ,ConcurrentModificationException will be thrown.

import java.util.*;
public class VectorSample {
private List list = null;
public VectorSample() {
list = new Vector();
}
public void addItems() {
list.add("Road");
list.add("Car");
list.add("Bus");
list.add("Truck");
}
public void iterate() {
Iterator itr = list.iterator();
while (itr.hasNext()){
list.remove(itr.next());
}
System.out.println("Now size = "+list.size());
}

public static void main(String[] args) {
VectorSample sample = new VectorSample();
sample.addItems();
sample.iterate();
}
}

 

In this case we are iterating the list and along with we are trying to remove items in from the list  .The output will be a ConcurrentModifictaionException .If we change the statement in constructor to

list = Collections.synchronizedList(new ArrayList())

then the concurrent wrapper is coming to picture.In this case also ConcurrentModificationException will be thrown.

In Java 1.5 CopyOnWriteArrayList and ConcurrentHashMap were introduced.Their iteration is also thread safe. Hence they are fully thread safe.

In the above  code if we change the statement in the constructor to

list = new CopyOnWriteArrayList()

then the output never shows ConcurrentModificationException.Output gives 0 as the list size.So  CopyOnWriteArrayList and ConcurrentHashMap are completely thread safe .The iteration is made as thread safe by lock stripping technique .

2)It avoids the performance downfall of legacies and concurrent wrappers.In legacies and concurrent wrappers  , if there is large volume of data then ,  locking that list for long duration is taking place . In case of  CopyOnWriteArrayList and ConcurrentHashMap  a segment of data is locking for an operation.Thus the performance is better in case of CopyOnWriteArrayList and ConcurrentHashMap.

Summary

Concurrent collections are completely thread  safe collections. They don’t have the performance downfall existing in legacies and concurrent wrapper classes.

See Related Topics:

Collections in Java

Threading in Java

Leave a Reply

Your email address will not be published. Required fields are marked *