Java - Future.get()多次调用

Mar*_*tto 22 java future

Future.get()在任务完成后多次调用Java时,Java的行为如何?它会返回相同的结果吗?或者,ExecutionException如果计算失败,会一次又一次地抛出相同的异常吗?我在文档中找不到任何关于它的东西!

Jes*_*per 31

你可以叫get()Future经常只要你喜欢,如果产生结果的任务还没有完成它只会阻止.

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

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


Nee*_*ain 6

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

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

V get()
throws InterruptedException,
      ExecutionException
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

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