Hi Stuart,

In general I agree with you regarding synchronized collections. But
nevertheless there is a lot of legacy code that still uses synchronized
wrappers. And sometimes synchronization is done on wrong lock object, which
leads to relatively rare but extremely hard to reproduce and troubleshoot
errors. Reworking whole this legacy stuff is risky. But if we had means to
get the right lock object for given synchronized collections, this would
help us to make the fixes more localized and hence safe. That's why I would
like to know the reason, why this has not been done earlier, and is there
hope/plan this will be done in near future.

Thank you,
Dmytro

On Thu, Apr 30, 2020 at 6:36 AM Stuart Marks <stuart.ma...@oracle.com>
wrote:

> 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