Partager via


Collections.SynchronizedSet(ICollection) Method

Definition

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

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

Parameters

s
ICollection

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

Returns

a synchronized view of the specified set.

Attributes

Remarks

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

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

Set s = Collections.synchronizedSet(new HashSet());
                 ...
             synchronized (s) {
                 Iterator i = s.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 set will be serializable if the specified set is serializable.

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