Share via


Collections.SynchronizedNavigableMap(INavigableMap) Method

Definition

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

[Android.Runtime.Register("synchronizedNavigableMap", "(Ljava/util/NavigableMap;)Ljava/util/NavigableMap;", "", ApiSince=26)]
[Java.Interop.JavaTypeParameters(new System.String[] { "K", "V" })]
public static Java.Util.INavigableMap SynchronizedNavigableMap (Java.Util.INavigableMap m);
[<Android.Runtime.Register("synchronizedNavigableMap", "(Ljava/util/NavigableMap;)Ljava/util/NavigableMap;", "", ApiSince=26)>]
[<Java.Interop.JavaTypeParameters(new System.String[] { "K", "V" })>]
static member SynchronizedNavigableMap : Java.Util.INavigableMap -> Java.Util.INavigableMap

Parameters

m
INavigableMap

the navigable map to be "wrapped" in a synchronized navigable map

Returns

a synchronized view of the specified navigable map.

Attributes

Remarks

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

It is imperative that the user manually synchronize on the returned navigable 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:

NavigableMap m = Collections.synchronizedNavigableMap(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:

NavigableMap m = Collections.synchronizedNavigableMap(new TreeMap());
             NavigableMap m2 = m.subMap(foo, true, bar, false);
                 ...
             Set s2 = m2.keySet();  // Needn't be in synchronized block
                 ...
             synchronized (m) {  // Synchronizing on m, not m2 or s2!
                 Iterator i = s.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 navigable map will be serializable if the specified navigable map is serializable.

Added in 1.8.

Java documentation for java.util.Collections.synchronizedNavigableMap(java.util.NavigableMap<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