【Tech-Android-Other】Android中的Future

来源:互联网 发布:matlab多变量数据拟合 编辑:程序博客网 时间:2024/05/17 23:25
public interface

Future

java.util.concurrent.Future<V>Known Indirect Subclasses
FutureTask<V>A cancellable asynchronous computation. RunnableFuture<V>A Future that isRunnable. RunnableScheduledFuture<V>A ScheduledFuture that isRunnable. ScheduledFuture<V>A delayed result-bearing action that can be cancelled. 

Class Overview

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 methodget when the computation has completed, blocking if necessary until it is ready. Cancellation is performed by thecancel 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 aFuture for the sake of cancellability but not provide a usable result, you can declare types of the formFuture<?> and return null as a result of the underlying task.

 
 
 
   这个类能获取异步预算的结果。它的方法可以提供我们去检查如果运算已经完成了,去等待它运算,去得到他的运算结果。
   这个运算结果仅仅在运算结束后通过get的方法获得,这个方法会阻塞程序。取消的运算是通过cancle这个方法呈现的。额外的方法提供了判断如果这个任务已经正常的完成或者已经被取消了。
   一旦一个运算操作已经完成,这个运算操作就不能被取消。如果想去使用一个Future的cncle功能,但又没有提供一个可用的结果,你可以声明一个类型通过Future<T> 和返回一个null值作为一个已经没有效力的任务。
 
 
 
 
public class

FutureTask

extends Object
implements RunnableFuture<V>
 
 

Class Overview

A cancellable asynchronous computation. This class provides a base implementation ofFuture, with methods to start and cancel a computation, query to see if the computation is complete, and retrieve the result of the computation. The result can only be retrieved when the computation has completed; theget method will block if the computation has not yet completed. Once the computation has completed, the computation cannot be restarted or cancelled.

A FutureTask can be used to wrap a Callable orRunnable object. BecauseFutureTask implements Runnable, a FutureTask can be submitted to anExecutor for execution.

In addition to serving as a standalone class, this class provides protected functionality that may be useful when creating customized task classes.

 

   一个可取消的异步运算,这个类实现了Fututre的借口,可以开始和取消一个运算操作,判断这个操作是否已经完成了,和重新得到运算操作的结果。运算的结果仅仅在运算结束完成后被恢复,get的方法将会阻塞程序,如果这个运算操作还没有完成的话。一旦运算操作完成了,这个操作就不能重新开始或者取消了。

 

   这个类过去常常用于包装一个Callable or Runnable 类的。因为FutureTask实现了runnable借口,一个FutureTask可以实现线程池中的execution方法。

   除了是一个单独的类之外,这个类还提供了可能有之用途定制任务classed的保护功能.

 

 

 

 
 
原创粉丝点击