何时使用 Callable 对象在 Java Executor 中调用 call() 方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2461819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:17:18  来源:igfitidea点击:

When does the call() method get called in a Java Executor using Callable objects?

javaconcurrency

提问by MalcomTucker

This is some sample code from an example. What I need to know is when call()gets called on the callable? What triggers it?

这是从一些示例代码示例。我需要知道的是什么时候call()在 callable 上被调用?什么触发它?

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);
  }
}

回答by Christopher Oezbek

Once you have submittedthe callable, the executor will schedule the callable for execution. Depending on the executor this might happen directly or once a thread becomes available.

一旦有了submitted可调用对象,执行程序就会安排可调用对象执行。根据执行程序的不同,这可能会直接发生,也可能会在线程可用时发生。

Calling geton the other hand only waits to retrieve the result of the computation.

get另一方面,调用只等待检索计算结果。

So to be precise: Somewhere in-between submitbeing called and the call to getreturning, the callable is called.

所以准确地说:在submit被调用和get返回调用之间的某个地方,调用了可调用对象。

回答by Joachim Sauer

The entire idea of using an Executoris that you shouldn't care when exactlythe method is called.

使用 an 的整个想法Executor是您不应该关心何时调用该方法。

The only thing that is guaranteed in general is that the method will have executed when get()of the Futurereturns.

一般来说,唯一可以保证的是该方法将get()Future返回时执行。

When exactly it will be called depends on which Executoryou use. With the fixed thread pool you use in the example, the call()method will be called as soon as there is a free thread and no other task is in front of the given task in the queue (so as long as there are enough tasks, you'll have 3 call()method calls running at any given time in your example).

确切的调用时间取决于Executor您使用的方法。使用您在示例中使用的固定线程池,call()只要有空闲线程并且队列中给定任务前面没有其他任务(因此只要有足够的任务,您就可以调用该方法)call()在您的示例中,将在任何给定时间运行3 个方法调用)。

回答by Ovidiu Lupas

The answer to "when will a callable be schedules" lies in java.util.concurrent.ThreadPoolExecutor#execute implementation (the default)

“什么时候可调用是调度”的答案在于 java.util.concurrent.ThreadPoolExecutor#execute 实现(默认)