Android AsyncTask Essentials

Essentials系列主要是讲原理和实现,应用可以参考API说明和APIDemo
AsyncTask是android自带的,用于异步调用的一个东西。
别人的轮子
上原版说明。
AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread. An asynchronous task is defined by 3 generic types, called Params, Progress and Result, and 4 steps, called begin, doInBackground, processProgress and end.

原版说明写的蛮好的,3 types, 4 steps.
这里我就不具体说明了,看看Reference就好了。
这里主要还是介绍多线程的应用

Android上面的确有很多需要用到多线程的地方。这个东西很好用。

下面就研究源码
主要先看几个成员变量

    /**  一个BlockingQueue,给ThreadPoolExecutor存Task用的。 */
    private static final BlockingQueue<Runnable> sWorkQueue =
            new LinkedBlockingQueue<Runnable>(10);
  
    /**  一个ThreadFactory,也是给ThreadPoolExecutor用的,计数功能,标识Thread id功能。 */
    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());
        }
    };
  
    /** ThreadPool*/
    private static final ThreadPoolExecutor sExecutor = new ThreadPoolExecutor(CORE_POOL_SIZE,
            MAXIMUM_POOL_SIZE, KEEP_ALIVE, TimeUnit.SECONDS, sWorkQueue, sThreadFactory);

    /** callable, 用于返回thread执行的结果 */
    private final WorkerRunnable<Params, Result> mWorker;
   
    //future 和task的混合体。construct method可以传入callable.
   // 可以用于获得thread执行结果,cancel thread.
    private final FutureTask<Result> mFuture;

constructor
initialize mWorker, mFuture.

execute()
        onPreExecute();  // run in UI thread

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

then invoke
call() in mWorker = new ...
        mWorker = new WorkerRunnable<Params, Result>() {
            public Result call() throws Exception {
                Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
                return doInBackground(mParams);   // run in new thread
            }
        };


then invoke
done() in mFuture = new ...
        mFuture = new FutureTask<Result>(mWorker) {
            @Override
            protected void done() {
                Message message;
                Result result = null;

                try {
                    result = get();
                } catch (InterruptedException e) {
                    android.util.Log.w(LOG_TAG, e);
                } catch (ExecutionException e) {
                    throw new RuntimeException("An error occured while executing doInBackground()",
                            e.getCause());
                } catch (CancellationException e) {
                    message = sHandler.obtainMessage(MESSAGE_POST_CANCEL,
                            new AsyncTaskResult<Result>(AsyncTask.this, (Result[]) null));
                    message.sendToTarget();
                    return;
                } catch (Throwable t) {
                    throw new RuntimeException("An error occured while executing "
                            + "doInBackground()", t);
                }

                message = sHandler.obtainMessage(MESSAGE_POST_RESULT,
                        new AsyncTaskResult<Result>(AsyncTask.this, result));
                message.sendToTarget();
            }
        };


Let us look up inner class InternalHandler

    private static class InternalHandler extends Handler {
        @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;
                case MESSAGE_POST_CANCEL:
                    result.mTask.onCancelled();
                    break;
            }
        }
    }

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

再看
    protected final void publishProgress(Progress... values) {
        sHandler.obtainMessage(MESSAGE_POST_PROGRESS,
                new AsyncTaskResult<Progress>(this, values)).sendToTarget();
    }

至此4 steps都看完了。

AsyncTask让你很安全的管理线程。

执行任务,取消任务,实时更新progress, 获得任务执行结果。

别人的轮子,可以拿来用。


看了下,

    private static final ThreadPoolExecutor sExecutor = new ThreadPoolExecutor(CORE_POOL_SIZE,

            MAXIMUM_POOL_SIZE, KEEP_ALIVE, TimeUnit.SECONDS, sWorkQueue, sThreadFactory);

AsyncTask里面的thread pool是static的。

这就是说用AsyncTask,一个APP里面正能同时跑5个thread.另外新启动的要在后面排队。

你可能感兴趣的:(thread,多线程,android,UI,UP)