I fail to understand why this code won’t compile
ExecutorService executor = new ScheduledThreadPoolExecutor(threads); class DocFeeder implements Callable<Boolean> {....} ... List<DocFeeder> list = new LinkedList<DocFeeder>(); list.add(new DocFeeder(1)); ... executor.invokeAll(list);
The error msg is:
The method invokeAll(Collection<Callable<T>>) in the type ExecutorService is not applicable for the arguments (List<DocFeeder>)
list is a Collection of DocFeeder, which implements Callable<Boolean> – What is going on?!
Just to expand on saua’s answer a little…
In Java 5, the method was declared as:
In Java 6, the method is declared as:
The wildcarding difference is very important – because
List<DocFeeder>is aCollection<? extends Callable<T>>but it’s not aCollection<Callable<T>>. Consider what would happen with this method:That’s legal – but it’s clearly bad if you can call
addSomethingwith aList<DocFeeder>as it will try to add a non-DocFeeder to the list.So, if you are stuck with Java 5, you need to create a
List<Callable<Boolean>>from yourList<DocFeeder>.