future

简介

java.util.concurrent包下的一个接口
主要作用是提供了一些方法来检查计算是否完成,等待其完成并检索计算结果。计算完成后,只能使用get来检索结果;取消计算可以使用cancell

这个接口里面主要有几个方法

boolean cancel(boolean mayInterruptIfRunning);
boolean isCancelled();
boolean isDone();
V get() throws InterruptedException, ExecutionException;
V get(long timeout, TimeUnit unit)
        throws InterruptedException, ExecutionException, TimeoutException;

futrureTask

任务状态

//初始化状态
private static final int NEW          = 0;
//正在完成中
private static final int COMPLETING   = 1;
//正常
private static final int NORMAL       = 2;
//异常状态
private static final int EXCEPTIONAL  = 3;
//取消
private static final int CANCELLED    = 4;
//中断中
private static final int INTERRUPTING = 5;
//中断结束状态
private static final int INTERRUPTED  = 6;

常用方法

public V get() throws InterruptedException, ExecutionException {
    int s = state;
    if (s <= COMPLETING)
        s = awaitDone(false, 0L);
    return report(s);
}
posted @ 2020-09-22 21:58  shyshare  阅读(97)  评论(0)    收藏  举报