这是一个示例中的示例代码.我需要知道的是什么时候call()
调用可调用的?触发它的是什么?
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);
}
}
Run Code Online (Sandbox Code Playgroud)
一旦有了submitted
callable,执行程序就会调度callable来执行.根据执行程序的不同,这可能会直接发生,也可能在线程可用后发生.
get
另一方面,调用只等待检索计算结果.
所以准确地说:在submit
调用和调用get
返回之间的某个地方调用callable.
归档时间: |
|
查看次数: |
9665 次 |
最近记录: |