0

我是否需要定期调用 future.get 来检查任务是否执行?

ExecutorService executor = Executors.newSingleThreadExecutor();

@SuppressWarnings("unchecked")
public void start(final file avo) throws IOException
{
    Future future = executor.submit(new Callable(){

    @Override
    public Object call() throws Exception {
        Condee conv = new Condee();
        return conv.doChange(file);
    });

    LOG.debug("Finished the execution serverion");
    executor.shutdown();


}
4

3 回答 3

2

要检查完成情况,您可以使用future.isDone(),这需要不断检查。

但是如果你使用future.get(),这个方法会等到完成然后返回结果。

于 2013-06-05T00:12:12.793 回答
1

在 executor.shutdown(); 之前添加对此的调用

future.get();

此方法仅在完成后返回。

于 2013-06-05T00:09:50.770 回答
0

Try to retrieve the value sing the get method. You will either get the result or an exception will be thrown. Here are the four possibilities when you call the get on future:

  1. You get the result value
  2. You get CancellationException - if the computation was cancelled (e.g. Future.cancel(true) is called)
  3. You get ExecutionException - if the computation threw an exception
  4. You get InterruptedException - if the current thread was interrupted while waiting (e.g. executor.shutdownNow() is called)
于 2013-06-05T00:21:47.047 回答