What's the use of generic in Callable
interface?
Consider this code which I copied from https://blogs.oracle.com/CoreJavaTechTips/entry/get_netbeans_6:
import java.util.\*;
import java.util.concurrent.\*;
public class CallableExample {
public static class WordLengthCallable
implements Callable {
private String word;
public WordLengthCallable(String word) {
this.word = word;
}
public Integer call() {
return Integer.valueOf(word.length());
}
}
public static void main(String args[]) throws Exception {
ExecutorService pool = Executors.newFixedThreadPool(3);
Set<Future<Integer>> set = new HashSet<Future<Integer>>();
for (String word: args) {
Callable<Integer> callable = new WordLengthCallable(word);
Future<Integer> future = pool.submit(callable);
set.add(future);
}
int sum = 0;
for (Future<Integer> future : set) {
sum += future.get();
}
System.out.printf("The sum of lengths is %s%n", sum);
System.exit(sum);
}
}
Integer in Callable<Integer>
and Future<Integer>
is not being used. It could be anything else as well like Callable<String>
and Future<String>
and code will still work.
I understand usage of Generics and appreciates its usage specially in collections.
Thanks.
get
will return anInteger
rather than anObject
, or am I wrong?