3
javadoc
/**
* Returns a synchronized (thread-safe) sorted set backed by the
specified
* sorted set. In order to guarantee serial access, it is critical that
* all 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 iterating over it or any of its subSet,
* headSet, or tailSet views.
*
* 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.
*
* @param the class of the objects in the set
* @param s the sorted set to be "wrapped" in a synchronized sorted set.
* @return a synchronized view of the specified sorted set.
*/
public static SortedSet synchronizedSortedSet(SortedSet s) {
return new SynchronizedSortedSet<>(s);
}