且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

invokeAll()不愿意接受Collection< Callable< T>>

更新时间:2021-10-20 17:00:00

只是为了扩展saua的答案......

Just to expand on saua's answer a little...

在Java 5中,方法是声明为:

In Java 5, the method was declared as:

invokeAll(Collection<Callable<T>> tasks) 

在Java 6中,该方法声明为:

In Java 6, the method is declared as:

invokeAll(Collection<? extends Callable<T>> tasks) 

通配符差别非常大重要 - 因为列表< DocFeeder> 集合<?扩展Callable< T>> 但它 Collection< Callable< T>> 。考虑一下这种方法会发生什么:

The wildcarding difference is very important - because List<DocFeeder> is a Collection<? extends Callable<T>> but it's not a Collection<Callable<T>>. Consider what would happen with this method:

public void addSomething(Collection<Callable<Boolean>> collection)
{
    collection.add(new SomeCallable<Boolean>());
}

这是合法的 - 但如果你能打电话给 addSomething 带有列表< DocFeeder> ,因为它会尝试将非DocFeeder添加到列表中。

That's legal - but it's clearly bad if you can call addSomething with a List<DocFeeder> as it will try to add a non-DocFeeder to the list.

因此,如果您遇到Java 5,则需要从列表< Callable< Boolean>> >列表< DocFeeder> 。

So, if you are stuck with Java 5, you need to create a List<Callable<Boolean>> from your List<DocFeeder>.