ServiceLoader double iterator issues

Posted by buge on Stack Overflow See other posts from Stack Overflow or by buge
Published on 2010-04-07T15:34:18Z Indexed on 2010/04/07 15:53 UTC
Read the original article Hit count: 171

Filed under:
|

Is this a known issue? I had trouble finding any search results.

When iterating over a ServiceLoader while an iteration already is in progress, the first iteration will be aborted. For example, assuming there are at least two implementations of Foo, the following code will fail with an AssertionError:

ServiceLoader<Foo> loader = ServiceLoader.load(Foo.class);
Iterator<Foo> iter1 = loader.iterator();
iter1.next();

Iterator<Foo> iter2 = loader.iterator();
while (iter2.hasNext()) {
    iter2.next();
}

assert iter1.hasNext();

This only seems to occur, if the second iterator really terminates. The code will succeed in this variation for example:

ServiceLoader<Foo> loader = ServiceLoader.load(Foo.class);
Iterator<Foo> iter1 = loader.iterator();
iter1.next();

Iterator<Foo> iter2 = loader.iterator();
iter2.next();

assert iter1.hasNext();

Is this a bug or a feature? :p

Is there a ticket for this already anywhere?

© Stack Overflow or respective owner

Related posts about java

Related posts about bug