Callable interface

I am leaning concurrency programming in jdk 5, and ran into trouble with Executor framework, actually, it's a generic type problem.

I want to use Executor.invokeAll to execute a collection of tasks, the signature of this method is:

<T> List<Future><T>> invokeAll(Collection<Callable><T>> tasks,long timeout, TimeUnit unit)

My task class is:

class LongTaskimplements Callable<Integer>{

...

}

My problems are:

1) I have to define the collection of the tasks as:

List<Callable><Integer>> tasks=new ArrayList<Callable><Integer>>();

I can't just say:

List<LongTask> tasks=new ArrayList<LongTask>();

If I do this, compiler will refuse to compile the following method:

exec.invokeAll(tasks,(long)6500, TimeUnit.MILLISECONDS);

I get it that List<LongTask> is not a subset of List<Callable><Integer>>, my question is why executor.invokeAll is not defined as the following:

<T> List<Future><T>> invokeAll(Collection<?extends Callable<T>> tasks,long timeout, TimeUnit unit)

If it is defined this way, then caller code will have a more natual way of using it.

2) My second question is how can i define a subclass of Callable whose call() method returns void? I tried "class LongTask implements Callable<Void>" and "class LongTask implements Callable<?>", but compiler is not happy with both of them.

Thanks.

[1882 byte] By [emeraldcpa] at [2007-11-27 9:59:09]
# 1
ad1)This is a known bug and fixed at least from Java6 up.See: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6267833ad2)You cannot. Your call method would have to return Void, if that is your intent.
stefan.schulza at 2007-7-13 0:30:03 > top of Java-index,Core,Core APIs...
# 2
how to return a Void?
emeraldcpa at 2007-7-13 0:30:03 > top of Java-index,Core,Core APIs...
# 3

public class ReturnVoid implements Callable<Void> {

Void call() {

return null;

}

}

stefan.schulza at 2007-7-13 0:30:04 > top of Java-index,Core,Core APIs...