Synchronized collections

Besides concurrent collections, we also have synchronized collections. Java provides a suite of wrappers that expose a collection as a thread-safe collection. These wrappers are available in Collections. The most common ones are as follows:

  • synchronizedCollection​(Collection<T> c): Returns a synchronized (thread-safe) collection backed by the specified collection
  • synchronizedList​(List<T> list): Returns a synchronized (thread-safe) list backed by the specified list:
List<Integer> syncList 
= Collections.synchronizedList(new ArrayList<>());
  • synchronizedMap​(Map<K,​V> m): Returns a synchronized (thread-safe) map backed by the specified map:
Map<Integer, Integer> syncMap 
= Collections.synchronizedMap(new HashMap<>());
  • synchronizedSet​(Set<T> s): Returns a synchronized (thread-safe) set backed by the specified set:
Set<Integer> syncSet 
= Collections.synchronizedSet(new HashSet<>());
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.56.12