开发者

ServiceLoader double iterator issues

开发者 https://www.devze.com 2022-12-25 18:36 出处:网络
开发者_JS百科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 e

开发者_JS百科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?


This could be a bug. I think it is since iterator.next() invokes next() on same reference of a lazy iterator internally. This behavior violates "Principle of least astonishment"

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号