AsyncTask的原理和缺点

番外tips: 特别喜欢一句话。假设你想了解一个人。那你从他出生開始了解吧。相同的道理,想要了解AsyncTask,就从它的源代码開始吧。

进入主题前,在说一下,开发中已经非常少使用AsyncTask了。如今流行的网络框架性能和使用都比AsyncTask好。但通过面试中遇到的一些老程序猿喜欢问这个问题,所以以下開始去分析。

public abstract class AsyncTask<Params, Progress, Result>

从声明来看,AsyncTask是一个抽象泛型类。我们都知道。我们创建AsyncTask的时候,经常处理几个方法

onPreExecute() //此方法在在主线程运行。用于后台任务进行前做一些准备工作
doInBackground(Params... params) //此方法在子线程运行,用来处理后台任务。像网络请求。耗时处理等操作
onProgressUpdate(Progress... values) //此方法在在主线程运,在doInBackground通过publishProgress来调用,用来更新进度
onPostExecute(Result result) //此方法在在主线程运行,后台任务处理完成调用。并返回后台任务的结果

回到AsyncTask上,在这个类中有这么段代码

static {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE_SECONDS, TimeUnit.SECONDS,
                sPoolWorkQueue, sThreadFactory);
        threadPoolExecutor.allowCoreThreadTimeOut(true);
        THREAD_POOL_EXECUTOR = threadPoolExecutor;
    }
//--------------------------------------------------不华丽的切割线
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); break; } } }

静态代码块的作用我就不赘述了,我要说的是,AsyncTask内部也是通过线程池+Handler的方式实现的,这样一说似乎大家瞬间理解了,可是,它内部是非常复杂的。我们继续往下看AsyncTask的构造方法:

 public AsyncTask() {
        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);
                }
            }
        };
    }

在构造方法中new了两个非常重要的对象,以下看一下两个类的声明。需说明一下,WorkerRunnable是在AsyncTask定义的一个抽象泛型类。

private static abstract class WorkerRunnable<Params, Result> implements Callable<Result> //WorkerRunnable实现了Callable接口

public class FutureTask<V> implements RunnableFuture<V>
public interface RunnableFuture<V> extends Runnable, Future<V> 
//FutureTask说白了就是一个Runnable对象

回到构造方法,在new WorkerRunnable 对象的时候通过result = doInBackground(mParams);给result赋值。然后会调用postResult(result)方法。看一下这种方法

 private Result postResult(Result result) {
        @SuppressWarnings("unchecked")
        Message message = getHandler().obtainMessage(MESSAGE_POST_RESULT,
                new AsyncTaskResult<Result>(this, result));
        message.sendToTarget();
        return result;
    }
    这种方法通过Message把result发送到Handler中,Handler终于传回onPostExecute(result)这种方法中。这里是通过前面声明Handler的时候,调用finish这种方法的,顺便看一下finish方法的源代码

    private void finish(Result result) {
        if (isCancelled()) {
            onCancelled(result);
        } else {
            onPostExecute(result);
        }
        mStatus = Status.FINISHED;
    }

,并把WorkerRunnable对象当做參数传递给FutureTask,既然传到FutureTask中了。以下就不继续看FutureTask 源代码先了。由于那是Runnable对象(里面就是进入run方法,完了然后set方法。赋值给result,最后在AsyncTask通过get()调用FutureTask 对象的get方法获取result)当然,AsyncTask是通过线程池来处理的。当我们创建完AsyncTask的时候。通过调用AsyncTask的execute方法,里面 通过exec.execute(mFuture)开启线程池去跑任务。跑完后回调FutureTask的done()方法。这种方法又 调用postResultIfNotInvoked(get())方法。这里是调用AsyncTask的get()方法从而调用FutureTask 对象的get方法获取result。

以下看看 execute方法的源代码。

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

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

通过上面的回想,AsyncTask的分析也基本完了。上述比較乱。但无非是AsyncTask的那几个经常用法何时被调用的。我再总结一下这个过程。首先是通过AsyncTask的构造方法初始化了两个对象,各自是WorkerRunnable和FutureTask,在WorkerRunnable中的call()方法通过result = doInBackground(mParams)这种方法调用doInBackground(mParams)方法,这里在说明一下,并不是在WorkerRunnable运行doInBackground方法,而是在FutureTask中,传入WorkerRunnable对象,然后通过调用AsyncTask的execute方法,把传入的FutureTask參数交给线程池去运行。

在这个execute方法中,调用executeOnExecutor方法,这种方法 中运行了onPreExecute()方法。当线程池跑完了后,回调FutureTask的done方法。done方法中调用AsyncTask的get()方法从而调用FutureTask 对象的get方法获取result并通过postResultIfNotInvoked(result)这种方法。这种方法中又调用postResult(result)方法,这种方法通过Message把result传递到Handler中。在Handler中调用onPostExecute(result)。终于把结果返回。这里另一个onProgressUpdate方法,这里在看一下源代码吧

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

在此,原理分析完成。以下这两篇文章分析了它的缺点。写得非常好。大家去阅读以下,这里仅转载过来做參考并总结,文章末尾给出链接。

1、线程池中已经有128个线程,缓冲队列已满。假设此时向线程提交任务,将会抛出RejectedExecutionException。

过多的线程会引起大量消耗系统资源和导致应用FC的风险。

2、AsyncTask不会随着Activity的销毁而销毁。直到doInBackground()方法运行完成。假设我们的Activity销毁之前。没有取消 AsyncTask。这有可能让我们的AsyncTask崩溃(crash)。由于它想要处理的view已经不存在了。所以,我们总是必须确保在销毁活动之前取消任务。

假设在doInBackgroud里有一个不可中断的操作,比方BitmapFactory.decodeStream(),调用了cancle() 也未必能真正地取消任务。关于这个问题。在4.4后的AsyncTask中,都有推断是取消的方法isCancelled(),可能參考的这些作者都分析较早的版本号。当然,这是笔者落后的原因。

3、假设AsyncTask被声明为Activity的非静态的内部类。那么AsyncTask会保留一个对创建了AsyncTask的Activity的引用。

假设Activity已经被销毁,AsyncTask的后台线程还在运行。它将继续在内存里保留这个引用,导致Activity无法被回收,引起内存泄露。

4、屏幕旋转或Activity在后台被系统杀掉等情况会导致Activity的又一次创建,之前运行的AsyncTask会持有一个之前Activity的引用,这个引用已经无效,这时调用onPostExecute()再去更新界面将不再生效。

參考博客出处
mylzc - AsyncTask的缺陷

viclee108 -AsyncTask的缺陷和问题

posted @ 2017-08-01 19:38  lytwajue  阅读(860)  评论(0编辑  收藏  举报