小白挑战:AsyncTask源码分析

//AsyncTask从本质上讲,是对ThreadPool和handler的封装.

在学习线程池相关的知识时,看到书中提到AsyncTask的实现中使用到了ThreadPool,于是把源码翻了出来,
源码并不长,顺便分析了一下.小白挑战,大牛勿喷(好像大牛也不会看到...)

成员变量的解析

我打算先把成员变量分析一下(AsyncTask源码不是很复杂,可以这么做,算是特殊情况吧),算是为后面分析AsyncTask的工作流程做铺垫.

线程池 THREAD_POOL_EXECUTOR

    //cpu核心数
    private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
    //核心线程数
    private static final int CORE_POOL_SIZE = CPU_COUNT + 1;
    //最大线程数
    private static final int MAXIMUM_POOL_SIZE = CPU_COUNT * 2 + 1;
    //非核心线程数闲置时的超时时长
    private static final int KEEP_ALIVE = 1;
    
    //线程工厂,为线程池提供创建新线程的功能只有一个方法newThread(),具体实现就是创建一个thread并返回
    private static final ThreadFactory sThreadFactory = new ThreadFactory() {
        private final AtomicInteger mCount = new AtomicInteger(1);

        public Thread newThread(Runnable r) {
            return new Thread(r, "AsyncTask #" + mCount.getAndIncrement());
        }
    };
    
    //线程池中的任务队列,通过线程池的execute方法提交的Runnable对象会存储在这个参数中,此处LinkedBlockingQueue是一个单向链表实现的阻塞队列
    private static final BlockingQueue sPoolWorkQueue =
            new LinkedBlockingQueue(128);

    //使用以上参数创建的线程池
    public static final Executor THREAD_POOL_EXECUTOR
            = new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE,
                    TimeUnit.SECONDS, sPoolWorkQueue, sThreadFactory);

sDefaultExecutor (特别强调:串行)

    private static class SerialExecutor implements Executor {
        //这里使用了一个双端队列
        final ArrayDeque mTasks = new ArrayDeque();
        Runnable mActive;

        public synchronized void execute(final Runnable r) {
            mTasks.offer(new Runnable() {
                public void run() {
                    try {
                        r.run();
                    } finally {
                        scheduleNext();
                    }
                }
            });
            if (mActive == null) {
                scheduleNext();
            }
        }

        protected synchronized void scheduleNext() {
            if ((mActive = mTasks.poll()) != null) {
                THREAD_POOL_EXECUTOR.execute(mActive);
            }
        }
    }

这里实现了一个串行的exectuor,这里的实现很巧妙,仔细观察可以发现,每次调用execute()方式都必然会调用scheduleNext(),
使得mTasks中的所有Runnable对象依次执行.

还有一点需要注意的是:
这里的execute()方法的作用只是用于给任务排队,真正执行任务的是THREAD_POOL_EXECUTOR.

然后看一下成员变量的声明:

    public static final Executor SERIAL_EXECUTOR = new SerialExecutor();

    private static volatile Executor sDefaultExecutor = SERIAL_EXECUTOR;

SERIAL_EXECUTOR 唯一的作用是把SerialExecutor对象传递给sDefaultExecutor,我觉得这种写法很奇怪,于是尝试了一下发现
final和volatile不能同时修饰一个变量,其实从它们各自的作用也可以看出同时使用这两个修饰符是矛盾的.

为什么这里要这样使用呢?

我觉得原因在于下面这个隐藏方法:

    /** @hide */
    public static void setDefaultExecutor(Executor exec) {
        sDefaultExecutor = exec;
    }

也就是说AsyncTask中有一个sDefaultExecutor的默认实现,但是还提供了一个隐藏方法,可以对其进行修改.

sHandler

首先先看一个内部类的实现,这个内部类是用来包装执行结果的,其中封装了一个AsyncTask实例和某种类型的数据集.

    @SuppressWarnings({"RawUseOfParameterizedType"})
    private static class AsyncTaskResult {
        final AsyncTask mTask;
        final Data[] mData;

        AsyncTaskResult(AsyncTask task, Data... data) {
            mTask = task;
            mData = data;
        }
    }

接下来看InternalHandler的具体实现

    private static final int MESSAGE_POST_RESULT = 0x1;//返回结果
    private static final int MESSAGE_POST_PROGRESS = 0x2;//返回进度

    private static InternalHandler sHandler;

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

        @SuppressWarnings({"unchecked", "RawUseOfParameterizedType"})
        @Override
        public void handleMessage(Message msg) {
            AsyncTaskResult result = (AsyncTaskResult) msg.obj;
            switch (msg.what) {
                case MESSAGE_POST_RESULT:
                    // There is only one result
                    result.mTask.finish(result.mData[0]);//具体实现在下面
                    break;
                case MESSAGE_POST_PROGRESS:
                    result.mTask.onProgressUpdate(result.mData);//这个就是我们自定义AsyncTask时需要实现的onProgressUpdate()
                    break;
            }
        }
    }

    private void finish(Result result) {
        if (isCancelled()) {
            onCancelled(result);//这个同样需要用户来实现
        } else {
            onPostExecute(result);//这个就是我们自定义AsyncTask时需要实现的onPostExecute()
        }
        mStatus = Status.FINISHED;//任务执行完成的状态
    }

    //这里的实现是单例模式
    private static Handler getHandler() {
        synchronized (AsyncTask.class) {
            if (sHandler == null) {
                sHandler = new InternalHandler();
            }
            return sHandler;
        }
    }

onProgressUpdate,onPostExecute,onCancelled这三个回调方法会在这里集中处理,结合AsyncTask的使用方法,我们可以得出以下结论:
在doInBackground()执行过程中或者结束后,会由该handler发送消息到主线程,调用相关回调方法,可以认为,handler是AsyncTask整个工作流程的倒数第二站.

mWorker&FutureTask

    private final WorkerRunnable mWorker;
    private final FutureTask mFuture;

    private static abstract class WorkerRunnable implements Callable {
        Params[] mParams;
    }
Callable

WorkRunnable实现了Callble接口,Callable与Runnable的功能大致相似,不同的是Callable是一个泛型接口,它有一个泛型参数V,该接口中只有一个call()方法,该方法返回类型为V的值.
Callable可以简单理解为有返回值的Runnable.
可以对比一下

public interface Callable {
    /**
     * 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;
}
public interface Runnable {

    /**
     * Starts executing the active part of the class' code. This method is
     * called when a thread is started that has been created with a class which
     * implements {@code Runnable}.
     */
    public void run();
}
FutureTask

FutureTask稍微复杂一些,FutureTask实现了 RunnableFuture接口,而RunnableFuture继承Runnable, Future

Future提供了对Runnable或者Callable任务的执行结果进行取消,查询是否完成,获取结果的操作,FutureTask是它的实现类

public interface Future {

    boolean cancel(boolean mayInterruptIfRunning);

    boolean isCancelled();

    boolean isDone();

    V get() throws InterruptedException, ExecutionException;

    V get(long timeout, TimeUnit unit)
        throws InterruptedException, ExecutionException, TimeoutException;
}

下面是FutureTask的一个构造函数,在AsyncTask的构造函数中会使用其创建一个FutureTask实例

    public FutureTask(Callable callable) {
        if (callable == null)
            throw new NullPointerException();
        this.callable = callable;
        this.state = NEW;       // ensure visibility of callable
    }

下面看一下构造函数对这两个成员变量的初始化(构造函数唯一做的事情)

    /**
     * Creates a new asynchronous task. This constructor must be invoked on the UI thread.
     */
    public AsyncTask() {
        mWorker = new WorkerRunnable() {
            public Result call() throws Exception {
                mTaskInvoked.set(true);//设置mTask已经被调用过

                Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
                //noinspection unchecked
                Result result = doInBackground(mParams);//调用doInBackground()!!!!!!!!
                Binder.flushPendingCommands();
                //调用postResult将结果投递给UI线程
                return postResult(result);
            }
        };

        //将mWorker作为参数创建FutureTask实例
        //在mFuture实例中,将会调用mWorker做后台任务,完成后调用done方法
        mFuture = new FutureTask(mWorker) {
            @Override
            protected void done() {
                try {
                    postResultIfNotInvoked(get());//确保postResult的执行
                } 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);
                }
            }
        };
    }

    //确保postResult的执行
    private void postResultIfNotInvoked(Result result) {
        final boolean wasTaskInvoked = mTaskInvoked.get();  
        if (!wasTaskInvoked) {
            postResult(result);
        }
    }

    //这里最终的结果是调用onCancelled()或者onPostExecute()
    private Result postResult(Result result) {
        @SuppressWarnings("unchecked")
        Message message = getHandler().obtainMessage(MESSAGE_POST_RESULT,
                new AsyncTaskResult(this, result));
        message.sendToTarget();
        return result;
    }

doInBackground和handler的使用都在这里!!!!!

最后的三个成员变量(标志位)

    //枚举 执行状态
    private volatile Status mStatus = Status.PENDING;
    
    //是否取消
    private final AtomicBoolean mCancelled = new AtomicBoolean();
    //mTask是否被调用过
    private final AtomicBoolean mTaskInvoked = new AtomicBoolean();

    public enum Status {
        /**
         * Indicates that the task has not been executed yet.
         */
        PENDING,
        /**
         * Indicates that the task is running.
         */
        RUNNING,
        /**
         * Indicates that {@link AsyncTask#onPostExecute} has finished.
         */
        FINISHED,
    }
至此,AsyncTask中的所有成员变量分析完毕,各个关键回调方法的调用地点也都清楚了,下面我们从execute()方法入手,分析一下AsyncTask的工作流程.
    @MainThread
    public final AsyncTask execute(Params... params) {
        return executeOnExecutor(sDefaultExecutor, params);//这里的参数现在看起来已经很清晰了,sDefaultExecutor是用于将任务加入队列的executor,params是用户提供的参数.
    }

继续往下看executeOnExecutor

    @MainThread
    public final AsyncTask executeOnExecutor(Executor exec,
            Params... params) {
        //首先,判断状态,只有PENDING才能继续执行
        if (mStatus != Status.PENDING) {
            switch (mStatus) {
                case RUNNING:
                    throw new IllegalStateException("Cannot execute task:"
                            + " the task is already running.");
                case FINISHED:
                    throw new IllegalStateException("Cannot execute task:"
                            + " the task has already been executed "
                            + "(a task can be executed only once)");
            }
        }
        //修改状态为RUNNING
        mStatus = Status.RUNNING;

        onPreExecute();//onPreExectue首先执行

        //设置mWorker的参数
        mWorker.mParams = params;
        //这里的exec就是sDefaultExecutor,
        exec.execute(mFuture);

        return this;
    }

通过上文的分析,我们知道这里的exec.execute(mFuture)最终调用到了THREAD_POOL_EXECUTOR中的execute()方法.
线程池内部的具体细节暂不分析,execute()方法执行的最终结果是将mFuture中的run()方法启动.

FutureTask中的run():

 public void run() {
        if (state != NEW ||
            !U.compareAndSwapObject(this, RUNNER, null, Thread.currentThread()))
            return;
        try {
            Callable c = callable;
            if (c != null && state == NEW) {
                V result;
                boolean ran;
                try {
                    result = c.call();//这里调用了mWorker的call()方法,而mWorker的call()调用了doInBackground()方法.至此,线程真正启动了.
                    ran = true;
                } catch (Throwable ex) {
                    result = null;
                    ran = false;
                    setException(ex);
                }
                if (ran)
                    set(result);//成功获取结果后,调用set()方法
            }
        } finally {
            // runner must be non-null until state is settled to
            // prevent concurrent calls to run()
            runner = null;
            // state must be re-read after nulling runner to prevent
            // leaked interrupts
            int s = state;
            if (s >= INTERRUPTING)
                handlePossibleCancellationInterrupt(s);
        }
    }

    protected void set(V v) {
        if (U.compareAndSwapInt(this, STATE, NEW, COMPLETING)) {
            outcome = v;
            U.putOrderedInt(this, STATE, NORMAL); // final state
            finishCompletion();//set()方法又调用finishCompletion()方法
        }
    }

 private void finishCompletion() {
        // assert state > COMPLETING;
        for (WaitNode q; (q = waiters) != null;) {
            if (U.compareAndSwapObject(this, WAITERS, q, null)) {
                for (;;) {
                    Thread t = q.thread;
                    if (t != null) {
                        q.thread = null;
                        LockSupport.unpark(t);
                    }
                    WaitNode next = q.next;
                    if (next == null)
                        break;
                    q.next = null; // unlink to help gc
                    q = next;
                }
                break;
            }
        }

        done();//finishCompletion()方法最终调用了done()方法,而上文中已经提到,调用done()方法最终的结果是通过handler调用onCancelled()或者opPostExecute();

        callable = null;        // to reduce footprint
    }

至此,AsyncTask的整个工作流程分析完毕,一次奇妙的源码之旅get,接下来可以去拜读各位大牛对于AsyncTask更加深入的分析了...

转载于:https://www.cnblogs.com/happyhacking/p/5343448.html

你可能感兴趣的:(ui,移动开发,runtime)