Thread safe Collection synchronizedCollection()


The methods of legacy classes (the data structure that comes with JDK 1.0) like Vector and Hashtable are implicitly synchronized. They are best suitable for thread-safe operations. An operation is said to be thread-safe when multiple threads access the data, the data should not get corrupted or result in inconsistency. It is the precaution to be taken in a multithreaded environment. But collection classes are not implicitly thread-safe as their methods are not synchronized. Here comes the Collections class for our rescue. It's synchronizedCollection() method returns an object of Collection with synchronized methods. That is with collection classes, you got a choice to have synchronized or non-synchronized version. But incase of legacy classes it is not possible; you have synchronized vector or hash table only.

Following is the method signature

  • static Collection synchronizedCollection(Collection col1): Returns a synchronized version of Collection col1. It is used for thread-safe operations. The synchronized version can be used safely in a multithreaded environment.

Methods like synchronizedSet(), synchronizedSortedSet(), synchronizedList(), synchronizedMap() and synchronizedSortedMap() also exist to use with set, list and map.

Following on Thread safe Collection synchronizedCollection() illustrates.
import java.util.*;
public class CollectionsSynchronized
{
  public static void main(String args[])
  {
    ArrayList firstList = new ArrayList();
    firstList.add(10);      
    firstList.add(20);
    firstList.add(30);      
    firstList.add(40);
    
    List secondList = Collections.synchronizedList(firstList);
    System.out.println("secondList elements: " + secondList);

    HashSet firstSet = new HashSet();
    firstSet.add("hello");
    firstSet.add("world");
    Set secondSet = Collections.synchronizedSet(firstSet);
    System.out.println("\nsecondSet elements: " + secondSet);
  }
}

Thread safe Collection synchronizedCollection()
Output screen of Thread safe Collection synchronizedCollection()

ArrayList firstList = new ArrayList();
firstList.add(10);

An ArrayList object firstList is created and added with few elements with add() method inherited from List interface. This object works nice in general conditions but not in multithreaded programming.

List secondList = Collections.synchronizedList(firstList);

The synchronizedList(firstList) method of Collections class returns an object of List, secondList. Now secondList methods are synchronized and suitable with multiple threads access. But still the original firstList methods not synchronized. Now programmer has the choice between the two, firstList and secondList, to use in his code.

HashSet firstSet = new HashSet();
firstSet.add(“hello”); firstSet.add(“world”);
Set secondSet = Collections.synchronizedSet(firstSet);

Similarly, from the HashSet object firstSet, it is possible to obtain a synchronized version with synchronizedSet(firstSet) method. The returned Set object, secondSet, is synchronized.

Leave a Comment

Your email address will not be published.