AsyncTask的线程池使用

这几天写了一个demo,利用asynctask来异步加载手机相册的照片,小米2S,4.1.1,但是发现相片是一张接一张加载出来的,而不是好几张同时加载的,说明我起的那些异步的线程在线程池里面排起队了,一个队,一个一个服务。

于是在网上搜了一下,才发现asynctask的线程池机制是跟系统版本有关系的,参考http://blog.csdn.net/hitlion2008/article/details/7983449

大致就是2.3以前的版本asynctask的线程池默认是Executors.newFixedThreadPool(5),即是5个服务的线程池,而3.0之后是由一个队列来维护的单队伍的线程池。

/**
     * An {@link Executor} that can be used to execute tasks in parallel.
     */
    public static final Executor THREAD_POOL_EXECUTOR
            = new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE,
                    TimeUnit.SECONDS, sPoolWorkQueue, sThreadFactory);

    /**
     * An {@link Executor} that executes tasks one at a time in serial
     * order.  This serialization is global to a particular process.
     */
    public static final Executor SERIAL_EXECUTOR = new SerialExecutor();
一个并行,一个串行。

private static volatile Executor sDefaultExecutor = SERIAL_EXECUTOR;
3.0之后默认的是串行的。

    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);
            }
        }
    }
串行线程池实现,就是维护了一个队列,等队头执行完了,再执行下一个,而干苦力的依然是并行的线程池
THREAD_POOL_EXECUTOR



所幸,asynctask提供了一个用户自己来指定线程池的方法

public final AsyncTask 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;
    }
所以使用的时候可以使用THREAD_POOL_EXECUTOR,也可以使用自己定义的线程池,如

private ExecutorService mExecutor = Executors.newFixedThreadPool(5);
    ……
private void loaddata(){
    ……
    task.executeOnExecutor(mExecutor, imageUrl);
}



你可能感兴趣的:(Android)