본문 바로가기

Java/__Base

Synchronization Wrappers

http://doc.ddart.net/java/tutorial/collections/implementations/wrapper.html


public static Collection synchronizedCollection(Collection c);
public static Set synchronizedSet(Set s);
public static List synchronizedList(List list);
public static Map synchronizedMap(Map m);
public static SortedSet synchronizedSortedSet(SortedSet s);
public static SortedMap synchronizedSortedMap(SortedMap m);

List list = Collections.synchronizedList(new ArrayList());


Collection c = Collections.synchronizedCollection(myCollection);
synchronized(c) {
    Iterator i = c.iterator(); // Must be in synchronized block!
    while (i.hasNext())
        foo(i.next());
}

Map m = Collections.synchronizedMap(new HashMap());
    ...
Set s = m.keySet();  // Needn't be in synchronized block
    ...
synchronized(m) {  // Synchronizing on m, not s!
    Iterator i = s.iterator(); // Must be in synchronized block
    while (i.hasNext())
        foo(i.next());
}

public static Collection unmodifiableCollection(Collection c); public static Set unmodifiableSet(Set s); public static List unmodifiableList(List list); public static Map unmodifiableMap(Map m); public static SortedSet unmodifiableSortedSet(SortedSet s); public static SortedMap unmodifiableSortedMap(SortedMap m);