1.

How Can We Synchronize A Collection Or How To Make A Collection Thread-safe?

Answer»

Collections class has several STATIC methods that RETURN synchronized collections. Each of the six core collection interfaces -Collection, SET, List, Map, SortedSet, and SortedMap - has one static factory method.

public static &LT;T> Collection<T> synchronizedCollection(Collection<T> c);
public static <T> Set<T> synchronizedSet(Set<T> s);
public static <T> List<T> synchronizedList(List<T> list);
public static <K,V> Map<K,V> synchronizedMap(Map<K,V> m);
public static <T> SortedSet<T> synchronizedSortedSet(SortedSet<T> s);
public static <K,V> SortedMap<K,V> synchronizedSortedMap(SortedMap<K,V> m); 

Each of these methods returns a synchronized (thread-safe) Collection backed up by the specified collection.

Collections class has several static methods that return synchronized collections. Each of the six core collection interfaces -Collection, Set, List, Map, SortedSet, and SortedMap - has one static factory method.

public static <T> Collection<T> synchronizedCollection(Collection<T> c);
public static <T> Set<T> synchronizedSet(Set<T> s);
public static <T> List<T> synchronizedList(List<T> list);
public static <K,V> Map<K,V> synchronizedMap(Map<K,V> m);
public static <T> SortedSet<T> synchronizedSortedSet(SortedSet<T> s);
public static <K,V> SortedMap<K,V> synchronizedSortedMap(SortedMap<K,V> m); 

Each of these methods returns a synchronized (thread-safe) Collection backed up by the specified collection.



Discussion

No Comment Found