Android--AsyncTask源码分析

2019-05-28  本文已影响0人  Czppp

闯关关卡---第9关,闯关进度20%,继续打怪升级

一直以来都没有使用过AsyncTask,且因为有Rxjava的存在。虽然说之前有看过AsyncTask知识包括,但是时间久了也忘记了,今天重新看了一下又有新的认知

1.AsyncTask大致的原理

AsyncTask是使用线程池,FutureTask,CallBack来执行任务,默认的任务执行顺序是先进先出(FIFO)

核心线程数:Math.max(2, Math.min(CPU_COUNT - 1, 4));
最大线程数:CPU个数x2+1
存活时间:30s--非核心线程的回收时间,若allowCoreThreadTimeOut设置为turn,也作用在核心线程中
任务队列:BlockingQueue<Runnable> sPoolWorkQueue =new LinkedBlockingQueue<Runnable>(128)

2.AsyncTask核心参数及方法

public abstract class AsyncTask<Params, Progress, Result> {...}

Params:异步任务参数
Progress:任务执行进度
Result:任务执行完返回结果
onPreExecute():任务执行前的准备工作--主线程
doInBackground(Params... params):开启执行任务,返回执行的任务结果--子线程
onProgressUpdate(Progress... values):任务执行的进度--主线程
onPostExecute(Result result):任务完成的结果处理,result为doInBackground返回值

3.AsyncTask准备工作--new AsyncTask()

    public AsyncTask() {
        this((Looper) null);
    }

    public AsyncTask(@Nullable Handler handler) {
        this(handler != null ? handler.getLooper() : null);
    }

    public AsyncTask(@Nullable Looper callbackLooper) {
        mHandler = callbackLooper == null || callbackLooper == Looper.getMainLooper()
            ? getMainHandler()
            : new Handler(callbackLooper);

        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
                    result = doInBackground(mParams);
                    Binder.flushPendingCommands();
                } catch (Throwable tr) {
                    mCancelled.set(true);
                    throw tr;
                } finally {
                    postResult(result);
                }
                return result;
            }
        };

        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);
                }
            }
        };
    }

在构造函数中,初始化Handler,实例化WorkerRunnable抽象类---实现CallBack接口,实例化FutureTask,重写done()方法,任务完成时发送Message,通过这些操作将任务装置在mFuture 中。下面是一个FutureTask简单例子

public class ThreadMain {

    public static void main(String[] args) {
        Callable<Integer> callable = new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                int i = 0;
                try {
                    for (; i < 2; i++) {
                        System.out.println(Thread.currentThread().getName() + " " + i);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    System.out.println("任务完成");
                }
                return i;
            }
        };
        FutureTask<Integer> task = new FutureTask<Integer>(callable) {
            @Override
            protected void done() {
                System.out.println("任务完成 done");
                super.done();
            }
        };
        task.run();
    }
}
image.png

4.任务执行

@MainThread
    public final AsyncTask<Params, Progress, Result> execute(Params... params) {
        return executeOnExecutor(sDefaultExecutor, params);
    }

    @MainThread
    public final AsyncTask<Params, Progress, Result> executeOnExecutor(Executor exec,
            Params... params) {
        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)");
            }
        }

        mStatus = Status.RUNNING;

        onPreExecute();

        mWorker.mParams = params;
        exec.execute(mFuture);

        return this;
    }

在任务第一次执行,mStatus 设置为RUNNING,调用onPreExecute()在任务执行前的准备工作,exec.execute(mFuture)开启异步任务。接下来看看 sDefaultExecutor 怎么实现的

5.sDefaultExecutor 实现方式

  private static class SerialExecutor implements Executor {
        final ArrayDeque<Runnable> mTasks = new ArrayDeque<Runnable>();
        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);
            }
        }
    }

sDefaultExecutor--默认任务处理器,ArrayDeque是一个双向的队列(FIFO), mTasks.offer()在最后添加一个任务元素,通过mTasks.poll()取得第一个任务元素使用THREAD_POOL_EXECUTOR线程池来执行任务。如果要并发处理调用executeOnExecutor(Executor exec,Params... params)方法,在参数1传入AsyncTask.THREAD_POOL_EXECUTOR

5.handler处理消息

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

        @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);
                    break;
            }
        }
    }

handlerMeaage主要任务处理结果还有任务进度,任务处理结果已经在构造函数实现了,但是发送任务进度消息需要调用publishProgress(Progress... values)来发送消息。

  protected final void publishProgress(Progress... values) {
        if (!isCancelled()) {
            getHandler().obtainMessage(MESSAGE_POST_PROGRESS,
                    new AsyncTaskResult<Progress>(this, values)).sendToTarget();
        }
    }
上一篇下一篇

猜你喜欢

热点阅读