➤ How to Code a Game
➤ Array Programs in Java
➤ Java Inline Thread Creation
➤ Java Custom Exception
➤ Hibernate vs JDBC
➤ Object Relational Mapping
➤ Check Oracle DB Size
➤ Check Oracle DB Version
➤ Generation of Computers
➤ XML Pros & Cons
➤ Git Analytics & Its Uses
➤ Top Skills for Cloud Professional
➤ How to Hire Best Candidates
➤ Scrum Master Roles & Work
➤ CyberSecurity in Python
➤ Protect from Cyber-Attack
➤ Solve App Development Challenges
➤ Top Chrome Extensions for Twitch Users
➤ Mistakes That Can Ruin Your Test Metric Program
Collections vs Concurrent Collections in Java | Following are the limitations of traditional collection (java.util package):-
- 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.
- 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.
- For any operations (even for read operations) complete collection object will be locked by one thread which creates a performance issue.
- 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:-
Main thread, current element: A
Child Thread updating list
Exception in thread “main” java.util.ConcurrentModificationException
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:-
- ConcurrentHashMap
- CopyOnWriteArrayList
- CopyOnWriteArraySet
Traditional Collection | Concurrent 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!