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

Martin Desruisseaux martin.desruisseaux at geomatys.fr
Tue Feb 24 13:47:28 UTC 2015


Hello Peter

Le 24/02/15 13:49, Peter Levart a écrit :
> You could synchronize on the entire loop and just copy over the
> service objects to another collection (say ArrayList).

Right, but I would like to keep the laziness instantiation behaviour.


> If you really must combine laziness of constructing service objects
> with concurrent iterations, you can wrap the ServiceLoader.iterator()
> with the following:
>
> public class BufferedConcurrentIterable<E> implements Iterable<E> {
> (...snip...)

Many thanks for your help. This is indeed what I did yesterday :-). But
given that the current ServiceLoader behaviour results in random
ConcurrentModificationException in some multi-threads environments, this
can mislead the users who believe that they have a synchronization error
in their own code before they realize that the issue is in ServiceLoader
itself.

    Martin





More information about the core-libs-dev mailing list