[concurrency-interest] Concurrency Wrappers

Curt at Tripos Curt at Tripos <ccox@tripos.com>
Wed, 25 Jun 03 09:43:18 -0500


Hi All,

The java.util.* collections have some concurrency support.
The collection implementations are unsynchronized.
Synchronization and protection against modification can
be added via several provided wrapper methods located in
java.util.Collections.

Collection synchronizedCollection(Collection c)
List       synchronizedList(List list)
Map        synchronizedMap(Map m)
Set        synchronizedSet(Set s)
SortedMap  synchronizedSortedMap(SortedMap m)
SortedSet  synchronizedSortedSet(SortedSet s)
Collection unmodifiableCollection(Collection c)
List       unmodifiableList(List list)
Map        unmodifiableMap(Map m)
Set        unmodifiableSet(Set s)
SortedMap  unmodifiableSortedMap(SortedMap m)
SortedSet  unmodifiableSortedSet(SortedSet s)

This pattern 
(interface + unsynchronized implementations + concurrency wrappers)
seems to work pretty well.  It allows testing different solutions
to concurrency problems by simply changing wrappers.

Given all of the new concurrency options about to be introduced,
I would welcome a factory class that generated an assortment of
concurrency wrappers.  I propose adding a wrapper class with at
least the following methods.   The goal is to make the most common
usage as simple as possible.

/**
Returns a synchronized (thread-safe) object backed
by the specified object.  The object returned implements the
same interfaces as the given object.
*/
public static Object synchronizedObject(Object object);

/**
Returns an object that is locked with the given lock and backed
by the specified object.  The object returned implements the
same interfaces as the given object.
*/
public static Object lockedObject(Lock lock, Object object);

Thanks,
Curt