WEB开发网
开发学院手机开发Android 开发 Android AsyncTask 阅读

Android AsyncTask

 2010-08-31 00:25:00 来源:WEB开发网   
核心提示:AsyncTask是android自带的,用于异步调用的一个东西,Android AsyncTask,别人的轮子上原版说明,AsyncTask enables proper and easy use of the UI thread. This class allows to perform background op

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 sWorkQueue =

new LinkedBlockingQueue(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 mWorker;

//future 和task的混合体。construct method可以传入callable.

// 可以用于获得thread执行结果,cancel thread.

1 2 3  下一页

Tags:Android AsyncTask

编辑录入:coldstar [复制链接] [打 印]
赞助商链接