When does the call() method get called in a Java Executor using Callable objects?
Posted
by MalcomTucker
on Stack Overflow
See other posts from Stack Overflow
or by MalcomTucker
Published on 2010-03-17T11:34:57Z
Indexed on
2010/03/17
11:41 UTC
Read the original article
Hit count: 261
java
|concurrency
This is some sample code from an example. What I need to know is when call()
gets called on the callable? What triggers it?
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); //**DOES THIS CALL call()?**
set.add(future);
}
int sum = 0;
for (Future<Integer> future : set) {
sum += future.get();//**OR DOES THIS CALL call()?**
}
System.out.printf("The sum of lengths is %s%n", sum);
System.exit(sum);
}
}
© Stack Overflow or respective owner