Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Synchronization Wrappers

 
meeta gaur
Ranch Hand
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Synchronization Wrappers

The synchronization wrappers add automatic synchronization (thread-safety) to an arbitrary collection. 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.


I'm wondering here, if we've synchronizedMap() and synchronizedSet() methods then what were need of synchronizedSortedSet() and synchronizedSortedMap() methods seperately ?


abcd

Couldn't synchronizedSet() make thread-safe SortedSet ? Now what is need of synchronizedSortedSet() here ?

And what about if i want to make thread-safe HashSet ? I believe SynchronizedSet() will work with it, as it did above.

http://docs.oracle.com/javase/tutorial/collections/implementations/wrapper.html
 
Manuel Petermann
Ranch Hand
Posts: 177
Hibernate Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you might already know SortedSet and SortedMap are Interfaces which extend the behaviour of their parent interfaces. Say you have a SortedSet variable an you need a syncronized one you could not do it without casting to SortedSet because synchronizedSet would return a normal Set. Those methods are mostly for the convenience.
 
meeta gaur
Ranch Hand
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the face of concurrent access, it is imperative that the user manually synchronize on the returned collection when iterating over it. The reason is that iteration is accomplished via multiple calls into the collection, which must be composed into a single atomic operation. The following is the idiom to iterate over a wrapper-synchronized collection.

Collection<Type> c = Collections.synchronizedCollection(myCollection);
synchronized(c) {
for (Type e : c)
foo(e);
}

If an explicit iterator is used, the iterator method must be called from within the synchronized block. Failure to follow this advice may result in nondeterministic behavior.


Why should i call iterator() inside synchronization block ?, is it not synchronized ?,if my backed Collection is synchronized then it must be also synchronized ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic