Java - Future.get()多次调用

时间:2015-02-21 17:33:52

标签: java future

Java Future.get()如何在任务完成后多次调用它的情况下表现如何? 它会返回相同的结果吗?或者,如果计算失败,会一次又一次地抛出一个ExecutionException吗? 我在文档中找不到任何关于它的内容!

2 个答案:

答案 0 :(得分:27)

您可以根据需要随时在get()上调用Future,只有在生成结果的任务尚未完成时才会阻止。

如果任务已经完成,它将立即返回任务的结果。

如果任务因异常而失败,则每次调用get()时都会调用ExecutionException

答案 1 :(得分:3)

  

我在文档中找不到任何关于它的内容!

你读过它们吗?因为当我读到它们时,我得到了答案,这里就是......

V get()
throws InterruptedException,
      ExecutionException
     

如果需要等待计算完成,然后检索   结果。

Returns:
    the computed result

Throws:
    CancellationException - if the computation was cancelled
    ExecutionException - if the computation threw an exception
    InterruptedException - if the current thread was interrupted while waiting

如果计算未完成,它将等待,如果它已经完成,它将尽快返回结果,无论你多少次调用它

相关问题