Share via


Collections.SynchronizedSortedMap(IDictionary) Method

Definition

Returns a synchronized (thread-safe) sorted map backed by the specified sorted map.

[Android.Runtime.Register("synchronizedSortedMap", "(Ljava/util/SortedMap;)Ljava/util/SortedMap;", "")]
[Java.Interop.JavaTypeParameters(new System.String[] { "K", "V" })]
public static System.Collections.IDictionary SynchronizedSortedMap (System.Collections.IDictionary m);
[<Android.Runtime.Register("synchronizedSortedMap", "(Ljava/util/SortedMap;)Ljava/util/SortedMap;", "")>]
[<Java.Interop.JavaTypeParameters(new System.String[] { "K", "V" })>]
static member SynchronizedSortedMap : System.Collections.IDictionary -> System.Collections.IDictionary

Parameters

m
IDictionary

the sorted map to be "wrapped" in a synchronized sorted map.

Returns

a synchronized view of the specified sorted map.

Attributes

Remarks

Returns a synchronized (thread-safe) sorted map backed by the specified sorted map. In order to guarantee serial access, it is critical that <strong>all</strong> access to the backing sorted map is accomplished through the returned sorted map (or its views).

It is imperative that the user manually synchronize on the returned sorted map when traversing any of its collection views, or the collections views of any of its subMap, headMap or tailMap views, via Iterator, Spliterator or Stream:

SortedMap m = Collections.synchronizedSortedMap(new TreeMap());
                 ...
             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());
             }

or:

SortedMap m = Collections.synchronizedSortedMap(new TreeMap());
             SortedMap m2 = m.subMap(foo, bar);
                 ...
             Set s2 = m2.keySet();  // Needn't be in synchronized block
                 ...
             synchronized (m) {  // Synchronizing on m, not m2 or s2!
                 Iterator i = s2.iterator(); // Must be in synchronized block
                 while (i.hasNext())
                     foo(i.next());
             }

Failure to follow this advice may result in non-deterministic behavior.

The returned sorted map will be serializable if the specified sorted map is serializable.

Java documentation for java.util.Collections.synchronizedSortedMap(java.util.SortedMap<K, V>).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to