Sdílet prostřednictvím


Collections.SynchronizedCollection(ICollection) Method

Definition

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

[Android.Runtime.Register("synchronizedCollection", "(Ljava/util/Collection;)Ljava/util/Collection;", "")]
[Java.Interop.JavaTypeParameters(new System.String[] { "T" })]
public static System.Collections.ICollection SynchronizedCollection (System.Collections.ICollection c);
[<Android.Runtime.Register("synchronizedCollection", "(Ljava/util/Collection;)Ljava/util/Collection;", "")>]
[<Java.Interop.JavaTypeParameters(new System.String[] { "T" })>]
static member SynchronizedCollection : System.Collections.ICollection -> System.Collections.ICollection

Parameters

c
ICollection

the collection to be "wrapped" in a synchronized collection.

Returns

a synchronized view of the specified collection.

Attributes

Remarks

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

It is imperative that the user manually synchronize on the returned collection when traversing it via Iterator, Spliterator or Stream:

Collection c = Collections.synchronizedCollection(myCollection);
                ...
             synchronized (c) {
                 Iterator i = c.iterator(); // Must be in the synchronized block
                 while (i.hasNext())
                    foo(i.next());
             }

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

The returned collection does not pass the hashCode and equals operations through to the backing collection, but relies on Object's equals and hashCode methods. This is necessary to preserve the contracts of these operations in the case that the backing collection is a set or a list.

The returned collection will be serializable if the specified collection is serializable.

Java documentation for java.util.Collections.synchronizedCollection(java.util.Collection<T>).

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