共同学习Java源代码-多线程与并发-FutureTask类(一)

这是个可以取消的未来异步任务。

public class FutureTask<V> implements RunnableFuture<V> 

这个类继承自RunnableFuture 实现了RunnableFuture接口


    private volatile int state;
    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;

这是任务的状态


    private Callable<V> callable;

这是Callable任务 也就是要运行的任务


    private Object outcome;

这是任务运行结果


    private volatile Thread runner;

这是运行那个Callable任务的线程 是volatile的


    private volatile WaitNode waiters;

这是这个FutureTask阻塞的线程集合 是个Treiber栈结构 treiber是德语工头 赶牲口的人的意思 这个WaitNode是FutureTask的内部类 


    private V report(int s) throws ExecutionException {
        Object x = outcome;
        if (s == NORMAL)
            return (V)x;
        if (s >= CANCELLED)
            throw new CancellationException();
        throw new ExecutionException((Throwable)x);
    }

这个是汇报的方法 参数是状态

如果参数是NORMAL就是正常 就返回outcome结果

如果参数是CANCELLED、INTERRUPTING、INTERRUPTED 就抛出异常


    public FutureTask(Runnable runnable, V result) {
        this.callable = Executors.callable(runnable, result);
        this.state = NEW;       // ensure visibility of callable
    }

这个是构造方法 传入一个Runnable对象和result

调用Executors类的callable方法对runnable和result进行包装 包装成Callable适配器 赋给callable

状态变成NEW


    public boolean isCancelled() {
        return state >= CANCELLED;
    }


    public boolean isDone() {
        return state != NEW;
    }

这两个就是重写了父类的判断状态的方法


    public boolean cancel(boolean mayInterruptIfRunning) {
        if (!(state == NEW &&
              UNSAFE.compareAndSwapInt(this, stateOffset, NEW,
                  mayInterruptIfRunning ? INTERRUPTING : CANCELLED)))
            return false;
        try {    // in case call to interrupt throws exception
            if (mayInterruptIfRunning) {
                try {
                    Thread t = runner;
                    if (t != null)
                        t.interrupt();
                } finally { // final state
                    UNSAFE.putOrderedInt(this, stateOffset, INTERRUPTED);
                }
            }
        } finally {
            finishCompletion();
        }
        return true;
    }

这个是取消方法 

判断 状态为NEW且调用UNSAFE的cas方法返回true 这两个条件有一个为false就返回false 其中UNSAFE的cas方法就是判断这个对象的状态是NEW的话 根据参数给这个对象的状态赋为INTERRUPTING或CANCELLED stateOffset是静态变量 在static块中初始化 其实我猜就是state变量的内存地址 

上面的判断不成立 也就是说状态为NEW 且 cas方法返回true 

就判断如果参数为true 也就是可以打断任务 那么调用runner线程 也就是运行Callable任务的线程的打断方法 中断线程 在finally块中 给stateOffset赋INTERRUPTED

最后调用finishCompletion方法执行后续操作

跳出判断和try块 返回true


    public V get() throws InterruptedException, ExecutionException {
        int s = state;
        if (s <= COMPLETING)
            s = awaitDone(false, 0L);
        return report(s);
    }

这个是get方法

先判断状态 如果是NEW或者COMPLETING 那就调用awaitDone方法传个false不计时等待任务执行完成 

否则直接调用report方法返回


    public V get(long timeout, TimeUnit unit)
        throws InterruptedException, ExecutionException, TimeoutException {
        if (unit == null)
            throw new NullPointerException();
        int s = state;
        if (s <= COMPLETING &&
            (s = awaitDone(true, unit.toNanos(timeout))) <= COMPLETING)
            throw new TimeoutException();
        return report(s);
    }

这个是有时限的get方法

判断第二个参数也就是时间单位为空 就抛出异常

然后判断状态如果是COMPLETING和NEW 且调用有时限的awaitDone方法在时限内还是返回COMPLETING或NEW 那就直接抛出异常代表任务超时 

最后调用report方法返回


    protected void done() { }

模板方法done



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值