Java Synchronized Collections

Synchronized Collections is nothing but a thread safe collection, to convert normal collection classes to a Synchronized Collections we have a method named as synchronizedCollection(Collection<> c) under the Collections class in Java

When we invoke the synchronizedCollection(Collection<> c) method by passing any Collection class Object, we get a synchronized collection. In synchronized collections all methods are thread safe or synchronized.

synchronizedCollection(Collection<> c) Method returns a new Class object named as SynchronizedCollection, all the methods of SynchronizedCollection class are thread safe.

public static Collection synchronizedCollection(Collection c) {
return new SynchronizedCollection<>(c);
}

synchronized Collection example

CopiedCopy Code
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
public class SynchronizedCollection {
public static void main(String[] args) {
List stringList=new ArrayList();
stringList.add("Synchronized");
stringList.add("Collections");
stringList.add("In");
stringList.add("Java");
Collection synchronizedCollection=Collections.synchronizedCollection(stringList);
System.out.println(synchronizedCollection);
}
}

Output:

[Synchronized, Collections, In, Java]