Collections vs Concurrent Collections in Java

Collections vs Concurrent Collections in Java | Following are the limitations of traditional collection (java.util package):-

  1. Most of the traditional collections are not thread-safe because at a time multiple threads are allowed to operate on collection objects which may lead to data inconsistency problems.
  2. Very few collections like Vector, hashtable, synchronizedList(), synchronizedSet(), and synchronizedMap() are thread-safe but the problem with these thread-safe collections is:- At a time only one thread is allowed to operate. 
  3. For any operations (even for read operations) complete collection object will be locked by one thread which creates a performance issue.
  4. While one thread iterates the collection object, the other threads are not allowed to perform any update operation on the underlying collection otherwise we will get ConcurrentModificationException.

Hence these traditional collection objects are not suitable for scalable for multithreaded applications. To overcome these problems concurrent collection was introduced in the 1.5 version.

Sample program to demonstrate ConcurrentModificationException while working with the collection.

While one thread is iterating the collection object, if the other thread is trying to modify the underlying collection object then it gives ConcurrentModificationException.

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Test extends Thread {
    private static List<String> list = new ArrayList<>();

    @Override
    public void run() {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
        }

        System.out.println("Child Thread updating list");
        list.add("Z");
    }

    public static void main(String[] args) throws InterruptedException {
        list.add("A");
        list.add("B");
        list.add("c");

        // create another thread
        Test test = new Test();
        test.start();

        // iterate list
        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()) {
            String string = iterator.next();
            System.out.println("Main thread, current element: " + string);
            Thread.sleep(3000);
        }

        System.out.println(list);
    }
}

Output:-

Difference between Traditional and Concurrent Collections

  • Concurrent collections are always thread-safe.
  • When compared with traditional thread-safe collections performance is more because of different locking mechanisms.
  • While one thread iterates the collection, the other threads are allowed to modify the collection in a safe manner.

Hence concurrent collections never throw ConcurrentModificationException. The important concurrent classes are:-

Traditional CollectionConcurrent Collection
Most of the collections are not thread-safe.Concurrent Collection is thread-safe.
Low Performance.High Performance.
While one thread iterates a traditional collection object, if the other thread is trying to modify the underlying collection object then it gives ConcurrentModificationException.Concurrent Collection never gives ConcurrentModificationException.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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