在Java中,对于异步操作结果的获取抽象出了一个接口Future<V>
,它提供了一些方法来检查或等待操作是否完成,以及获取操作的结果,同时还提供了取消操作的能力,以及提供了检测操作是正常结束还是被取消的方法。不过如果操作已经完成了,就不能再对其进行取消了。在某些场景下,如果只是想借助Future
实现操作取消的能力,而不关心操作结果,那么可以返回空即可。
A Future
represents the result of an asynchronous computation. Methods are provided to check if the computation is complete, to wait for its completion, and to retrieve the result of the computation.
The result can only be retrieved using method get
when the computation has completed, blocking if necessary until it is ready. Cancellation is performed by the cancel
method. Additional methods are provided to determine if the task completed normally or was cancelled. Once a computation has completed, the computation cannot be cancelled. If you would like to use a Future
for the sake of cancellability but not provide a usable result, you can declare types of the form Future<?>
and return null
as a result of the underlying task.