Aracılığıyla paylaş


Collections.SynchronizedSortedSet(ISortedSet) Method

Definition

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

[Android.Runtime.Register("synchronizedSortedSet", "(Ljava/util/SortedSet;)Ljava/util/SortedSet;", "")]
[Java.Interop.JavaTypeParameters(new System.String[] { "T" })]
public static Java.Util.ISortedSet SynchronizedSortedSet (Java.Util.ISortedSet s);
[<Android.Runtime.Register("synchronizedSortedSet", "(Ljava/util/SortedSet;)Ljava/util/SortedSet;", "")>]
[<Java.Interop.JavaTypeParameters(new System.String[] { "T" })>]
static member SynchronizedSortedSet : Java.Util.ISortedSet -> Java.Util.ISortedSet

Parameters

s
ISortedSet

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

Returns

a synchronized view of the specified sorted set.

Attributes

Remarks

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

It is imperative that the user manually synchronize on the returned sorted set when traversing it or any of its subSet, headSet, or tailSet views via Iterator, Spliterator or Stream:

SortedSet s = Collections.synchronizedSortedSet(new TreeSet());
                 ...
             synchronized (s) {
                 Iterator i = s.iterator(); // Must be in the synchronized block
                 while (i.hasNext())
                     foo(i.next());
             }

or:

SortedSet s = Collections.synchronizedSortedSet(new TreeSet());
             SortedSet s2 = s.headSet(foo);
                 ...
             synchronized (s) {  // Note: s, not s2!!!
                 Iterator i = s2.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 sorted set will be serializable if the specified sorted set is serializable.

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