If you are a beginner then read the collection framework and then move further. Because this post is directly related to collections of java. We have discussed all the collection classes and interface in recent posts.
Concurrent collections in java are designed and optimized specifically for synchronized multithreaded access. These are the thread safe collections, and these are existing in java.util.concurrent package. In this post, we will see all the java concurrent collections and find see how to use thread safe collections java.
Java 9 : Factory Methods for Immutable List, Set, Map and Map.Entry
Need of Concurrent Collections
As we know Java supports multithreading and concurrency, we achieve the synchronization by use of the synchronization keyword. The collection framework provides various collections and we can make them thread-safe by use of the synchronized keyword. But there are some problems that occur when we use the Collections concept in multi-threading without the synchronized keyword.
1. In the Collection framework most of the Collection classes are not thread-safe because those are non-synchronized. Like ArrayList, LinkedList, HashMap is non-synchronized in nature. If multiple threads will perform any operation on an object simultaneously. Then the data will not be in proper synchronized and leads to data redundancy
2. We can’t modify the collection while iterating the Collection object. If we try to modify the collection, it throws ConcurrentModificationException. So non-synchronized collections are not good choice for Multi-threaded applications.
To overcome these problems Java introduced a new feature in JDK 1.5 Version. It introduced some thread safe collections java.
Concurrent collections in java
Java concurrent collections were introduced in version 5 and part of the java.util.concurrent package. Here java introduced some more classes that are designed for concurrent access from multiple threads.
For example, HashMap is non-synchronized class but ConcurrentHashMap class is synchronized hash-based Map implementation.
Here we have some java Concurrent collection classes:
1. Immutable List in Java
2. CopyOnWriteArrayList in java
3. Immutable Set in Java
4. CopyOnWriteArraySet in java
5. ConcurrentHashMap in java
Java 9 has introduced some more ways to create immutable collections. You can read it here.
Factory Methods for Immutable List, Set, Map and Map.Entry
4 thoughts on “Concurrent collections in java”
Hello 🙂 Your post is very brilliant and fascinated, I like the idea and conce[tion. I retargeting main address for all friends Thanks!
Heya i am for the first time here. I found this board and I find It truly useful & it helped me out a lot. I hope to give something back and help others like you helped me.
I am glad and nice to be a visitant of this everlasting blog ! appreciate it for this rare information indeed!.
This really is such a fantastic useful resource that youre providing and also you give it away for free. I adore seeing web sites that understand the value of providing a top quality resource free of charge. It?s the outdated what goes around comes around program.