
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference between Traditional Collections and Concurrent Collections in java
In Java, as we know, Collections are one of the most important concepts that make Java a powerful language in itself. It's the support of collections in Java that makes it support any type of data in a convenient and efficient way, along with possible CRUD operations over them.
But on the same phase, when collections get exposed to a multi-threading its performance gets somewhat degraded because somewhere collections lack the support for a multi-threading environment. To overcome this limitation, Java introduces Concurrent Collections, which not only overcome the multi-threading environment limitation but also enhance Java to perform with multiple threads of data.
Concurrent Collections
The Concurrent Collections were introduced in Java 5 and are present in java.util.concurrent package. These collections help in providing thread safety and better performance, making it suitable for multi-threaded applications.
Difference between Traditional and Concurrent Collections
The following are the important differences between Traditional Collections and Concurrent Collections:
Key | Traditional Collections | Concurrent Collections |
---|---|---|
Thread Safety | Most of the classic classes in Java Collections, such as ArrayList, Linked List, HashMap, etc., are not synchronized and are not thread safe in a multi-threaded environment. | On the other hand, Java introduces the same classes in Concurrent Collections with implement synchronization in them, which not only makes these classes synchronized but also thread safe in nature. |
Locking Mechanism | We have some synchronized classes in traditional collections as well, such as Vector and Stack, but these classes use a lock over the whole collection, which reduces performance and speed of execution. | On the other hand, concurrent collections introduce the concept of partial locking, where they lock only part of the collection in case of multi-threading environment, which improves the performance and speed of collections in such an environment. |
Runtime Exception | In case of traditional collections, if we try to modify a collection through a separate thread during collection iteration, then we get a Runtime Exception, ConcurrentModificationException. | On the other hand, one would not get such an exception if it deals with the concurrent collections, i.e., concurrent collections allow modification in the collection during its iteration. |
Preference | Due to the reasons mentioned in the above points, traditional collections are not preferred in a multi-threading environment. | On the other hand, Concurrent collections are primarily preferred in a multi-threaded environment. |
Introduction to the market | Traditional collections are a type of legacy collection in Java and were introduced before concurrent collections. | While the Concurrent collections were introduced later, in JDK 1.5, to provide better support for multithreaded and concurrent programming. |