ConcurrentModificationException in java.util.ServiceLoader (not a multi-thread issue)

Martin Desruisseaux martin.desruisseaux at geomatys.fr
Tue Feb 24 09:32:36 UTC 2015


Le 24/02/15 09:09, Alan Bateman a écrit :

> Right, it has never supported multiple iterators but as it's an
> Iterable then it should unless specified otherwise. So I think this is
> a bug (although one could argue that the usage is unusual, almost a
> mis-use).

One use case is that in a multi-thread environment, this bug implies
that it is not sufficient to synchronize all direct and indirect
(through Iterator) usages of ServiceLoader. We must synchronize the
entire iteration loop for preventing other threads to start a new
iteration before we finished the current one. This can be annoying if
the work to do between two calls to Iterator.hasNext() / next() is long.
Sometime it is not possible to synchronize the entire loop if we do not
control the iteration (i.e. if we advance in the iterator only when the
user invokes some method). The later case can be a problem even in
mono-thread application.


> Not clear whether it's worth doing anything about it now, this is
> because ServiceLoader is going to change very significantly soon when
> it is re-specified to work with modules. I'll create a bug anyway to
> track it anyway.

Thanks. The fact that ServiceLoader was going to be modified for Jigsaw
is precisely the reason why I was wondering if it was worth to
investigate more now.

    Martin





More information about the core-libs-dev mailing list