Future及AsyncTask源码解读

一、FutureTask及Future的使用

一个可取消的异步计算,FutureTask提供了对Future的基本实现,可以调用方法去开始和取消一个计算,可以查询计算是否完成,可以获取异步计算的结果。

1、基本使用方式一
// FutureTask需要一个Callable对象
FutureTask<String> task = new FutureTask<>(new Callable<String>() {
    @Override
    public String call() throws Exception {
        Thread.sleep(5000);
        return "ABC";
    }
});
// 把FutureTask交给线程并开启即可
Thread thread = new Thread(task);
thread.start();
System.out.println("====thread start====");
try {
    // 获取计算结果,这个方法会阻塞当前线程的,直到结果返回为止
    String result = task.get();
    System.out.println("===" + result + "===");
} catch (Exception e) {
}
2、基本使用方式一
// 通过线程池的形式,提交一个Callable对象
ExecutorService executorService = Executors.newCachedThreadPool();
Future<String> future = executorService.submit(new Callable<String>() {
    @Override
    public String call() throws Exception {
        Thread.sleep(5000);
        return "ABC";
    }
});
try {
    // 获取计算结果,这个方法会阻塞当前线程的,直到结果返回为止
    String result = future.get();
    System.out.println("===" + result + "===");
} catch (Exception e) {
}
3、源码分析
/**
* 定义了一个接口
*/
public interface Callable<V> {
    /**
     * Computes a result, or throws an exception if unable to do so.
     *
     * @return computed result
     * @throws Exception if unable to compute a result
     */
    V call() throws Exception;
}

/**
* Future同样是一个接口
*/
public interface Future<V> {
    // 调用该方法即可以取消任务的执行。如果子任务还未开始执行,那么调用该
    // 方法子任务会被取消不会被执行。如果子任务正在执行中,就分两种情况,
    // 情况1是mayInterruptIfRunning=true,那么会中断任务的线程,并返回true
    // 情况2是mayInterruptIfRunning=false,不会中断任务的线程,但是还是会返回true
    // 如果子任务已执行结束或已被取消,就会返回false
    boolean cancel(boolean mayInterruptIfRunning);
    // 判断任务是否已取消
    boolean isCancelled();
    // 判断任务是否已结束,正常执行结束,或者发生异常结束,或者被取消,都属于结束
    boolean isDone();
    // 获取计算结果或者说子任务线程的返回结果
    V get() throws InterruptedException, ExecutionException;
    // 同上,因为这个方法会阻塞线程,参数的意思就表示最多阻塞多久就不再阻塞了。
    V get(long timeout, TimeUnit unit)
        throws InterruptedException, ExecutionException, TimeoutException;
}
/**
* FutureTask继承的接口RunnableFuture,相当于多封装一层,
* 这个接口既继承了Runnable,也继承了Future
*/
public interface RunnableFuture<V> extends Runnable, Future<V> {
    /**
     * Sets this Future to the result of its computation
     * unless it has been cancelled.
     */
    void run();
}
/**
* FutureTask类
*/
public class FutureTask<V> implements RunnableFuture<V> {
    // 状态变化有如下4种情况:
    // NEW -> COMPLETING -> NORMAL --------------------------------------- 正常执行结束的流程
    // NEW -> COMPLETING -> EXCEPTIONAL ---------------------执行过程中出现异常的流程
    // NEW -> CANCELLED -------------------------------------------被取消,即调用了cancel(false)
    // NEW -> INTERRUPTING -> INTERRUPTED -------------被中断,即调用了cancel(true)

    private volatile int state;
    // 默认的原始状态
    private static final int NEW          = 0;
    // 表示任务执行结束(正常执行结束,或者发生异常结束),但是还没有将结果保存到outcome中。是一个中间状态。
    private static final int COMPLETING   = 1;
    // 表示任务正常执行结束,并且已经把执行结果保存到outcome字段中。是一个最终状态。
    private static final int NORMAL       = 2;
    // 表示任务发生异常结束,异常信息已经保存到outcome中,这是一个最终状态。
    private static final int EXCEPTIONAL  = 3;
    // 任务在执行结束之前被取消了,但是不要求中断正在执行的线程,也就是调用了cancel(false)
    private static final int CANCELLED    = 4;
    // 任务在执行结束之前被取消了,并要求中断线程的执行,也就是调用了cancel(true)
    private static final int INTERRUPTING = 5;
    // 调用cancel(true)取消异步任务,会调用interrupt()中断线程的执行,然后状态会从INTERRUPTING变到INTERRUPTED。
    private static final int INTERRUPTED  = 6;
    
    /**
    * 构造方法,需要一个callable对象
    */
    public FutureTask(Callable<V> callable) {
        if (callable == null)
            throw new NullPointerException();
        this.callable = callable;
        this.state = NEW;       // ensure visibility of callable
    }
    
    /**
    * 交由线程后,会执行这个方法
    */
    public void run() {
        try {
            Callable<V> c = callable;
            if (c != null && state == NEW) {
                V result;
                boolean ran;
                try {
                    // 直接调用Callable的call方法,获取结果
                    result = c.call();
                    ran = true;
                } catch (Throwable ex) {
                    result = null;
                    ran = false;
                    setException(ex);
                }
                // 把结果存储到变量outcome中
                if (ran)
                    set(result);
            }
        } finally {
            runner = null;
            int s = state;
            if (s >= INTERRUPTING)
                handlePossibleCancellationInterrupt(s);
        }
    }
    
    /**
    * 获取结果的get方法,该方法会阻塞线程
    */
    public V get() throws InterruptedException, ExecutionException {
        int s = state;
        if (s <= COMPLETING)
            s = awaitDone(false, 0L);
        return report(s);
    }
    
    private int awaitDone(boolean timed, long nanos)
        throws InterruptedException {
        long startTime = 0L;    
        WaitNode q = null;
        boolean queued = false;
        for (;;) {
            //1. 读取状态
            //1.1 如果s > COMPLETING,表示任务已经执行结束,
            // 或者发生异常结束了,就不会阻塞,直接返回
            int s = state;
            if (s > COMPLETING) {
                if (q != null)
                    q.thread = null;
                return s;
            }
            //1.2 如果s == COMPLETING,表示任务结束(正常/异常),
            //但是结果还没有保存到outcome字段,当前线程让出执行权,给其他线程先执行
            else if (s == COMPLETING)
                // We may have already promised (via isDone) that we are done
                // so never return empty-handed or throw InterruptedException
                Thread.yield();
            //2. 如果调用get()的线程被中断了,就从等待的线程栈中移除
            //这个等待节点,然后抛出中断异常
            else if (Thread.interrupted()) {
                removeWaiter(q);
                throw new InterruptedException();
            }
            //3. 如果等待节点q=null,就创建一个等待节点
            else if (q == null) {
                if (timed && nanos <= 0L)
                    return s;
                q = new WaitNode();
            }
            //4. 如果这个等待节点还没有加入等待队列,就加入队列头
            else if (!queued)
                queued = U.compareAndSwapObject(this, WAITERS,
                        q.next = waiters, q);
            //5. 如果设置了超时等待时间
            else if (timed) {
                //5.1 设置startTime,用于计算超时时间,如果超时时间到了,就等待队列中移除当前节点
                final long parkNanos;
                if (startTime == 0L) { // first time
                    startTime = System.nanoTime();
                    if (startTime == 0L)
                        startTime = 1L;
                    parkNanos = nanos;
                } else {
                    long elapsed = System.nanoTime() - startTime;
                    if (elapsed >= nanos) {
                        removeWaiter(q);
                        return state;
                    }
                    parkNanos = nanos - elapsed;
                }
                // nanoTime may be slow; recheck before parking
                //5.2 如果超时时间还没有到,而且任务还没有结束,就阻塞特定时间
                if (state < COMPLETING)
                    LockSupport.parkNanos(this, parkNanos);
            }
            //6. 阻塞,等待唤醒
            else
                // LockSupport就是一个提供阻塞线程方法的工具类
                LockSupport.park(this);
        }
    }
}

二、AsyncTask源码

1、构造方法
public AsyncTask(@Nullable Looper callbackLooper) {
    // 初始化Handler
    mHandler = callbackLooper == null || callbackLooper == Looper.getMainLooper()
        ? getMainHandler()
        : new Handler(callbackLooper);
    
    // 其实是Callable
    mWorker = new WorkerRunnable<Params, Result>() {
        public Result call() throws Exception {
            mTaskInvoked.set(true);
            Result result = null;
            try {
                Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
                //noinspection unchecked
                // AsyncTask里重写的doInBackground
                result = doInBackground(mParams);
                Binder.flushPendingCommands();
            } catch (Throwable tr) {
                mCancelled.set(true);
                throw tr;
            } finally {
                // 最终会执行这个方法
                postResult(result);
            }
            return result;
        }
    };
    // 初始化FutureTask
    mFuture = new FutureTask<Result>(mWorker) {
        @Override
        protected void done() {
            try {
                postResultIfNotInvoked(get());
            } catch (InterruptedException e) {
                android.util.Log.w(LOG_TAG, e);
            } catch (ExecutionException e) {
                throw new RuntimeException("An error occurred while executing doInBackground()",
                        e.getCause());
            } catch (CancellationException e) {
                postResultIfNotInvoked(null);
            }
        }
    };
}
/**
* WorkerRunnable其实就是一个Callable,多了个变量mParams
*/
private static abstract class WorkerRunnable<Params, Result> implements Callable<Result> {
    Params[] mParams;
}

/*
* postResult方法会通过主线程的Handler把消息发出去
*/
private Result postResult(Result result) {
    Message message = getHandler().obtainMessage(MESSAGE_POST_RESULT,
            new AsyncTaskResult<Result>(this, result));
    message.sendToTarget();
    return result;
}

private static class InternalHandler extends Handler {
    public InternalHandler(Looper looper) {
        super(looper);
    }

    @Override
    public void handleMessage(Message msg) {
        AsyncTaskResult<?> result = (AsyncTaskResult<?>) msg.obj;
        switch (msg.what) {
            case MESSAGE_POST_RESULT:
                // 上面发送的消息会来到这里,调用下方的finish方法
                result.mTask.finish(result.mData[0]);
                break;
            case MESSAGE_POST_PROGRESS:
                result.mTask.onProgressUpdate(result.mData);
                break;
        }
    }
}

private void finish(Result result) {
    if (isCancelled()) {
        onCancelled(result);
    } else {
        // 调用我们重写的onPostExecute方法
        onPostExecute(result);
    }
    mStatus = Status.FINISHED;
}
2、execute开启AsyncTask执行
public final AsyncTask<Params, Progress, Result> execute(Params... params) {
    // 其实sDefaultExecutor是一个默认的线程池,并且是静态的,即所有的AsyncTask对象都公用一个默认的线程池
    // 等下再展示sDefaultExecutor的代码
    return executeOnExecutor(sDefaultExecutor, params);
}

public final AsyncTask<Params, Progress, Result> executeOnExecutor(Executor exec,
        Params... params) {

    mStatus = Status.RUNNING;
    // 回调onPreExecute
    onPreExecute();
    // 保存params到Callable的子类,即WorkerRunnable
    mWorker.mParams = params;
    // 调用线程池的execute方法,即子类sDefaultExecutor的execute方法
    exec.execute(mFuture);
    return this;
}

private static volatile Executor sDefaultExecutor = SERIAL_EXECUTOR;
public static final Executor SERIAL_EXECUTOR = new SerialExecutor();
private static class SerialExecutor implements Executor {
    // mTasks是一个双向队列
    final ArrayDeque<Runnable> mTasks = new ArrayDeque<Runnable>();
    Runnable mActive;
    // 这个方法即上面调用执行的execute方法,r即是我们的FutureTask
    // synchronized同步,保证串行
    public synchronized void execute(final Runnable r) {
        // 添加到队列的头部
        mTasks.offer(new Runnable() {
            public void run() {
                try {
                    r.run();
                } finally {
                    scheduleNext();
                }
            }
        });
        if (mActive == null) {
            scheduleNext();
        }
    }
    // 把下一个Runnable拿出来并执行
    // mTasks.poll会从队列里移除,并移除的值返回
    protected synchronized void scheduleNext() {
        if ((mActive = mTasks.poll()) != null) {
            // THREAD_POOL_EXECUTOR = ThreadPoolExecutor
            // THREAD_POOL_EXECUTOR实际上是1个已配置好的可执行并行任务的线程池
            THREAD_POOL_EXECUTOR.execute(mActive);
            // 调用后会执行mActive的Run方法,而上面的run方法会调用r.run();
             // 即调用mFutureTask的run()方法,熟悉FutureTask的都知道,最终会调用Callable的call方法
             // 最终去到构造方法里的mWorkRunnable
        }
    }
}
通过上面的分析可以得出以下结论:

(1)AsyncTask内部有两个线程池,sDefaultExecutor = SerialExecutor,该线程池的作用是任务调度,让多个AsyncTask任务按顺序执行,即串行。另外一个是THREAD_POOL_EXECUTOR是我们doInBackground执行的真正线程池。
(2)线程执行完毕后,依然是通过Handler发送消息给主线程去执行onPostExecute。
(3)调用executeOnExecutor时,我们可以传入自定义的线程池,那么就不会使用AsyncTask默认的sDefaultExecutor了。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值