Hi Dmytro,

Callers of an API performing explicit synchronization, along with the synchronized collections wrappers, have mostly fallen into disuse since the introduction of the java.util.concurrent collections.

Multiple threads can either interact directly on a concurrent collection, or the developer can provide an intermediate object (not a collection) that does internal locking, and that exports the right set of thread-safe APIs to callers. I'm thus skeptical of the utility of enhancing these wrapper classes with additional APIs.

Do you have a use case that's difficult to handle any other way?

s'marks



On 4/29/20 12:58 AM, dmytro sheyko wrote:
Hello,

Have you ever discussed to make field mutex in synchronized collections
accessible?

Javadoc for Collections#synchronizedSortedSet suggest to iterate collection
this way:

   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());
   }

I.e. in order to iterate subset, we also need a reference to the whole set,
which is not really convenient. How about to make it possible to write:

   SortedSet s2 = s.headSet(foo);
       ...
   synchronized (Collections.getSyncRoot(s2)) {  // Note:
Collections.getSyncRoot(s2)
       Iterator i = s2.iterator(); // Must be in the synchronized block
       while (i.hasNext())
           foo(i.next());
   }

Also I think it would be convenient to let to provide custom sync root when
synchronized collection is created.
E.g.

   Object customSyncRoot = new Object();
   SortedSet s = Collections.synchronizedSortedSet(new TreeSet(),
customSyncRoot);

What do you think about this?

Regards,
Dmytro

Reply via email to