Java Collections Synchronized Methods
November 19, 2023
On this page we will learn to use Java collections synchronized methods. Find the those methods.
synchronizedCollection()
synchronizedList()
synchronizedMap()
synchronizedSet()
synchronizedSortedMap()
synchronizedSortedSet()
1. Collections.synchronizedCollection
It synchronizes theCollection
instance. The instance returned by synchronizedCollection()
is now synchronized and is thread safe to add elements. But while iterating we should use synchronized block otherwise it may result in non-deterministic behavior.
SynchronizedCollectionDemo.java
package com.concretepage.util; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Iterator; public class SynchronizedCollectionDemo { public static void main(String[] args) { Collection<String> obj = new ArrayList<>(); obj.add("Mohan"); // not thread safe Collection<String> synObj = Collections.synchronizedCollection(obj); synObj.add("Ganesh"); // thread safe //Below iteration will result in non-deterministic behavior Iterator<String> ite1 = synObj.iterator(); while (ite1.hasNext()) { String s = ite1.next(); System.out.println(s); } //Below code is the right way to iterate synchronized collection synchronized(synObj) { Iterator<String> ite2 = synObj.iterator(); while (ite2.hasNext()) { String s = ite2.next(); System.out.println(s); } } } }
2. Collections.synchronizedList
It synchronizes theList
instance. The instance returned by synchronizedList()
will be synchronized and thread safe. But while iterating we should use synchronized block otherwise it may result in non-deterministic behavior.
SynchronizedListDemo.java
package com.concretepage.util; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; public class SynchronizedListDemo { public static void main(String[] args) { List<String> obj = new ArrayList<>(); obj.add("Mohan"); // not thread safe List<String> synObj = Collections.synchronizedList(obj); synObj.add("Ganesh"); // thread safe //Below iteration will result in non-deterministic behavior Iterator<String> ite1 = synObj.iterator(); while (ite1.hasNext()) { String s = ite1.next(); System.out.println(s); } //Below code is the right way to iterate synchronized List synchronized(synObj) { Iterator<String> ite2 = synObj.iterator(); while (ite2.hasNext()) { String s = ite2.next(); System.out.println(s); } } } }
3. Collections.synchronizedMap
It synchronizes theMap
instance. The instance returned by synchronizedMap()
will be synchronized and thread safe. But while iterating we should use synchronized block otherwise it may result in non-deterministic behavior.
SynchronizedMapDemo.java
package com.concretepage.util; import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; public class SynchronizedMapDemo { public static void main(String[] args) { Map<Integer,String> obj = new HashMap<>(); obj.put(1, "Mohan"); // not thread safe Map<Integer,String> synObj = Collections.synchronizedMap(obj); synObj.put(2, "Sohan"); // thread safe Set<Integer> set = synObj.keySet(); //thread safe //Below iteration will result in non-deterministic behavior Iterator<Integer> ite1 = set.iterator(); while (ite1.hasNext()) { String s = obj.get(ite1.next()); System.out.println(s); } //Below code is the right way to iterate synchronized Map synchronized(synObj) { Iterator<Integer> ite2 = set.iterator(); while (ite2.hasNext()) { String s = obj.get(ite2.next()); System.out.println(s); } } } }
4. Collections.synchronizedSet
It synchronizes theSet
instance. The instance returned by synchronizedSet()
will be synchronized and thread safe. But while iterating we should use synchronized block otherwise it may result in non-deterministic behavior.
SynchronizedSetDemo.java
package com.concretepage.util; import java.util.Collections; import java.util.HashSet; import java.util.Iterator; import java.util.Set; public class SynchronizedSetDemo { public static void main(String[] args) { Set<String> obj = new HashSet<>(); obj.add("Mohan"); // not thread safe Set<String> synObj = Collections.synchronizedSet(obj); synObj.add("Ganesh"); //thread safe //Below iteration will result in non-deterministic behavior Iterator<String> ite1 = synObj.iterator(); while (ite1.hasNext()) { String s = ite1.next(); System.out.println(s); } //Below code is the right way to iterate synchronized Set synchronized(synObj) { Iterator<String> ite2 = synObj.iterator(); while (ite2.hasNext()) { String s = ite2.next(); System.out.println(s); } } } }
5. Collections.synchronizedSortedMap
It synchronizes theSortedMap
instance. The instance returned by synchronizedList()
will be synchronized and thread safe. But while iterating we should use synchronized block otherwise it may result in non-deterministic behavior.
SynchronizedSortedMapDemo.java
package com.concretepage.util; import java.util.Collections; import java.util.Iterator; import java.util.Set; import java.util.SortedMap; import java.util.TreeMap; public class SynchronizedSortedMapDemo { public static void main(String[] args) { SortedMap<Integer,String> obj = new TreeMap<>(); obj.put(1, "Mohan"); // not thread safe SortedMap<Integer,String> synObj = Collections.synchronizedSortedMap(obj); synObj.put(2, "Sohan"); // thread safe Set<Integer> set = synObj.keySet(); //thread safe //Below iteration will result in non-deterministic behavior Iterator<Integer> ite1 = set.iterator(); while (ite1.hasNext()) { String s = obj.get(ite1.next()); System.out.println(s); } //Below code is the right way to iterate synchronized SortedMap synchronized(synObj) { Iterator<Integer> ite2 = set.iterator(); while (ite2.hasNext()) { String s = obj.get(ite2.next()); System.out.println(s); } } } }
6. Collections.synchronizedSortedSet
It synchronizes theSortedSet
instance. The instance returned by synchronizedList()
will be synchronized and thread safe. But while iterating we should use synchronized block otherwise it may result in non-deterministic behavior.
SynchronizedSortedSetDemo.java
package com.concretepage.util; import java.util.Collections; import java.util.Iterator; import java.util.SortedSet; import java.util.TreeSet; public class SynchronizedSortedSetDemo { public static void main(String[] args) { SortedSet<String> obj = new TreeSet<>(); obj.add("Mohan"); // not thread safe SortedSet<String> synObj = Collections.synchronizedSortedSet(obj); synObj.add("Ganesh"); // thread safe //Below iteration will result in non-deterministic behavior Iterator<String> ite1 = synObj.iterator(); while (ite1.hasNext()) { String s = ite1.next(); System.out.println(s); } //Below code is the right way to iterate synchronized SortedSet synchronized(synObj) { Iterator<String> ite2 = synObj.iterator(); while (ite2.hasNext()) { String s = ite2.next(); System.out.println(s); } } } }