Java’s Collection framework is one of the foundational components of the Java platform. Collections in represent a group of objects and the framework provides several interfaces and implementations to work with these collections. With such a rich framework, developers can focus on what matters the most! Java With the advent of multi-threaded programming, the need for thread-safe collections grew. It is when were added to Java’s Collection framework. Let us understand what makes Synchronized Collections thread-safe, and some things we should keep in mind while using them. Synchronized Collections How do Synchronized Collections ensure thread safety? Synchronized collections achieve thread-safety by enforcing synchronization on each of its publicly available methods. In addition to that, it ensures that its internal state is never published. Thus, the only way to modify a collection is via its public synchronized methods! Think of synchronized collections as plain unsynchronised collections plus state encapsulation and synchronized public methods. Since the same (intrinsic) lock guards every public method of a synchronized collection, no two threads can modify/read the collection at once. This ensures that the collection always maintains its variants and hence becomes thread-safe. How to create Synchronized Collections The class exposes several static methods for creating synchronized collections. These static methods are named in the following format — . Collections synchronizedXxx Here is a list of these methods: (Collection<T> c) synchronizedCollection (List<T> list) synchronizedList (Map<K, V> m) synchronizedMap (NavigableMap<K, V> m) synchronizedNavigableMap (NavigableSet<T> s) synchronizedNavigableSet (Set<T> s) synchronizedSet (SortedMap<K, V> m) synchronizedSortedMap (SortedSet<T> s) synchronizedSortedSet This is how you will create a synchronized list using the Collections class! List<Integer> synchronizedIntegerList = Collections.synchronizedList(new ArrayList<>()); Pitfall of Compound Actions on Synchronized Collections While methods exposed from synchronized collections are thread-safe, compound actions on client side still require proper locking. Consider the following method — public void putIfAbsent(List<Integer> synchronizedList, Integer elem) { if(!synchronizedList.contains()) { synchronizedList.add(elem); } } This innocent-looking method is ! Even with thread-safe and methods, the method does not achieve what it is intended to. Between the and steps of our method, another thread can add the to the list. Guarding such compound actions is the responsibility of the client. thread-unsafe contains add putIfAbsent ‘check’ ‘act’ elem Synchronized collections allow client-side locking to ensure that such compound actions are atomic concerning other operations. Each public method of the synchronized collection is guarded by its intrinsic lock. Thus, the client can create atomic operations by acquiring the same lock. Here is how we will fix using client-side locking. putIfAbsent public void putIfAbsent(List<Integer> synchronizedList, Integer elem) { synchronized(synchronizedList) { if(!synchronizedList.contains()) { synchronizedList.add(elem); } } } Pitfalls of Iterating on Synchronized Collections Some extra care is needed to be taken when iterating over a synchronized collection. When iterating over a synchronized collection using iterators, any concurrent modifications will cause the iterator to On detecting any concurrent modification, the iterator will throw a ConcurrentModificationException. fail fast. final Set<String> syncStringSet = Collections.synchronizedSet(new HashSet<>()); // Might throw ConcurrentModificationException for(String s: syncStringSet) { doSomething(s); } To avoid getting a ConcurrentModificationException, we can hold a lock on the synchronized collection for the entire duration of the iteration. Clearly, this is not the most performant approach since the iteration can take a long time and it will block other executing threads from accessing the collection! Synchronized classes provide an easy mechanism to make your collection classes thread-safe. A thorough understanding of these would help you avoid common pitfalls associated with it. With this, we reach the end of this blog. I hope you enjoyed reading this piece. Also published here.