Home  >  Core Java

Java Collections synchronizedCollection(), synchronizedList(), synchronizedMap(), synchronizedSet(), synchronizedSortedMap(), synchronizedSortedSet() Example

By Arvind Rai, July 09, 2016
On this page we will provide java Collections synchronizedCollection(), synchronizedList(), synchronizedMap(), synchronizedSet(), synchronizedSortedMap(), synchronizedSortedSet() methods example.

Collections.synchronizedCollection

It synchronizes the Collection 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);
			  }
		  }
	}
} 



Collections.synchronizedList

It synchronizes the List 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);
			  }
		  }
	}
} 

Collections.synchronizedMap

It synchronizes the Map 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.
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);
			  }
		  }
	}
} 

Collections.synchronizedSet

It synchronizes the Set 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);
			  }
		  }
	}
} 

Collections.synchronizedSortedMap

It synchronizes the SortedMap 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);
			  }
		  }
	}
} 

Collections.synchronizedSortedSet

It synchronizes the SortedSet 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);
			  }
		  }
	}
} 
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us