-
Notifications
You must be signed in to change notification settings - Fork 0
Concurrent Collection
Rohit Agarwal edited this page Jan 21, 2018
·
2 revisions
- Traditional collection object(like ArrayList, HashMap etc) can be accessed by multiple threads simultaneously and there may be chance of data inconsistency problem and hence these are not thread safe.
- Already existing thread safe collections(Vector, Hashtable, synchronizedList(), synchronizedSet(), synchronizedMap()) performance wise not up to the mark.
- Because of every operation even for read operation also, total collection will be loaded by only one thread at a time and it increases waiting time of threads.
- Another big problem with traditional collections is while one thread iterating collection, the other threads are not allowed to modify collection object simultaneously. If we are trying to modify then we will get ConcurrentModificationException.
- Hence these traditional collection objects are not suitable for scalable multithreaded applications.
- To overcome these problems sun people introduced Concurrent Collections in 1.5 version.
package com.javamultiplex.concurrentcollection;
import java.util.ArrayList;
import java.util.Iterator;
public class Example1 implements Runnable{
public static ArrayList<String> list=new ArrayList<>();
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Child thread updated");
list.add("D");
}
public static void main(String[] args) throws Exception{
list.add("A");
list.add("B");
list.add("C");
Runnable runnable=new Example1();
Thread thread=new Thread(runnable);
thread.start();
Iterator<String> iterator=list.iterator();
while(iterator.hasNext()) {
String string=iterator.next();
System.out.println("Main thread iterating list and current object is "+string);
Thread.sleep(3000);
}
}
}
Traditional | Concurrent |
---|---|
Not Thread Safe | Thread Safe |
Performance wise low | Performance wise high because different type of locking mechanism. |
ConcurrentModificationException thrown when one thread is iterating and other thread trying to update collection. | CocurrentModificationException never come. |
- ConcurrentHashMap
- CopyOnWriteArrayList
- CopyOnWriteArraySet
- Object putIfAbsent(Object key,Object value) - To add entry to the map if the specified key is not already available.
Method Defination
Object putIfAbsent(Object key,Object value) {
if(!map.containsKey(key)) {
map.put(key,value);
}else {
return map.get(key);
}
}
put() | putIfAbsent() |
---|---|
If key is already present old value will be replaced with new value and return old value | If the key is present then entry won't be added and return value associated with key. If key is not present then only entry will be added. |
Example
public class Example2 {
public static void main(String[] args) {
ConcurrentHashMap<Integer, String> map=new ConcurrentHashMap<>();
map.put(101, "Rohit");
map.put(101, "Raman");
System.out.println(map);//{101=Raman}
map.putIfAbsent(101, "javamultiplex");
System.out.println(map);//{101=Raman}
}
}