Collections.SynchronizedMap(IDictionary) Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Returns a synchronized (thread-safe) map backed by the specified map.
[Android.Runtime.Register("synchronizedMap", "(Ljava/util/Map;)Ljava/util/Map;", "")]
[Java.Interop.JavaTypeParameters(new System.String[] { "K", "V" })]
public static System.Collections.IDictionary SynchronizedMap (System.Collections.IDictionary m);
[<Android.Runtime.Register("synchronizedMap", "(Ljava/util/Map;)Ljava/util/Map;", "")>]
[<Java.Interop.JavaTypeParameters(new System.String[] { "K", "V" })>]
static member SynchronizedMap : System.Collections.IDictionary -> System.Collections.IDictionary
Parameters
the map to be "wrapped" in a synchronized map.
Returns
a synchronized view of the specified map.
- Attributes
Remarks
Returns a synchronized (thread-safe) map backed by the specified map. In order to guarantee serial access, it is critical that <strong>all</strong> access to the backing map is accomplished through the returned map.
It is imperative that the user manually synchronize on the returned map when traversing any of its collection views via Iterator
, Spliterator
or Stream
:
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());
}
Failure to follow this advice may result in non-deterministic behavior.
The returned map will be serializable if the specified map is serializable.
Java documentation for java.util.Collections.synchronizedMap(java.util.Map<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.