Home » Java programming language

Differences between Synchronized Collection and Concurrent Collection in Java

Synchronized Collection vs Concurrent Collection in Java: Here, we are going to learn what are the differences between Synchronized Collection and Concurrent Collection in Java programming language?
Submitted by Preeti Jain, on August 08, 2019

Synchronized Collection vs Concurrent Collection in Java

Here, we will see how Synchronized Collection differs from Concurrent Collection in Java?

Synchronized Collection

  • Now, we will see what is synchronize? Here, synchronize means only one thread is allowed to operate on an object at a time or in other words the object(which is synchronized) can't be modified by multiple threads simultaneously.
  • Synchronized Collection can be modified by one thread at a time(i.e. it is not possible to modify or access Synchronized Collection by multiple threads simultaneously).
  • Synchronized Collection has low performance than Concurrent Collection because at a time only one thread is allowed to operate on an object so it increases the waiting time of the threads.
  • Synchronized Collection acquires the lock on the entire Collection object which provides thread-safety.
  • SynchronizedMap is a static inner class of Collections class which is available in java.util.Collections.
  • In SynchronizedMap it acquires the lock on entire Map object and it wraps all the methods of Map interface with synchronized keyword.
  • SynchronizedMap may allow null keys and null values depend on the real Collections class.

Example:

import java.util.*;

class SynchronizedCollectionClass {
    public static void main(String[] args) {
        try {
            Set set = new HashSet();
            
            set.add(10);
            set.add(20);
            set.add(30);
            set.add(40);
            set.add(50);
            
            System.out.println("Current Set is :" + set);
            Collection collection = Collections.synchronizedCollection(set);
            System.out.println("Synchronized Collection is :" + set);
        } catch (IndexOutOfBoundsException ex) {
            System.out.println("Exception :" + ex.getMessage());
        }
    }
}

Output

E:\Programs>javac SynchronizedCollectionClass.java

E:\Programs>java SynchronizedCollectionClass
Current Set is :[50, 20, 40, 10, 30]
Synchronized Collection is :[50, 20, 40, 10, 30]

Here, we will see how Concurrent Collection differs from Synchronized Collection in Java?


Concurrent Collection

  • Now, we will see what is Concurrent? Here, concurrent means only multiple threads are allowed to operate on an object at a time or in other words the object (which is concurrent) can be modified by multiple threads simultaneously.
  • Concurrent Collection can be modified by multiple threads at a time(i.e. it is possible to modify or access Concurrent Collection by multiple threads simultaneously).
  • Concurrent Collection has high performance than Synchronized Collection because at a time multiple threads are allowed to operate on an object so it decreases the waiting time of the threads.
  • More than one threads can perform read-write operation concurrently still it provides Thread Safety.
  • ConcurrentHashMap is a class introduced in Java 5 that is available in java.util package.
  • ConcurrentHashMap divides Map object into different parts and every thread acquires the lock on every part.
  • As we know default concurrency level is 16 that means maximum 16 threads are allowed to access an object simultaneously by default and we can increase and decrease concurrency level if we want.
  • ConcurrentHashMap does not allow null keys and null values.

Example:

// We will see in a program why Concurrent concept is required

import java.util.*;

class ConcurrentClass extends Thread {
    static LinkedList ll = new LinkedList();
    public void run() {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            System.out.println("ConcurrentClass" +
                " will add element");
        }

        // By using add() method to add an element in Concurrent Class Thread
        ll.add("Java");
    }

    public static void main(String[] args)
    throws InterruptedException {
        ll.add("C");
        ll.add("C++");
        ll.add("Ruby");
        ll.add("Python");

        // Creating an instance of ConcurrentClass and it will modify 
        ConcurrentClass cc = new ConcurrentClass();
        cc.start();

        // Iterating LinkedList 
        Iterator iterator = ll.iterator();
        while (iterator.hasNext()) {
            String str = (String) iterator.next();
            System.out.println(str);
            Thread.sleep(8000);
        }
        System.out.println(ll);
    }
}

Output

E:\Programs>javac ConcurrentClass.java

E:\Programs>java ConcurrentClass
C
Exception in thread "main" java.util.ConcurrentModificationException
        at java.util.LinkedList$ListItr.checkForComodification
        (LinkedList.java:953)
        at java.util.LinkedList$ListItr.next(LinkedList.java:886)
        at ConcurrentClass.main(ConcurrentClass.java:34)






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.