bluedolphincp
Jul 4th, 2007, 07:35 PM
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 LongTask implements 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.
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 LongTask implements 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.