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()
[color=red]onPreExecute[/color](); // 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 [color=red]doInBackground[/color](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.[color=red]onProgressUpdate[/color](result.mData);
break;
case MESSAGE_POST_CANCEL:
result.mTask.onCancelled();
break;
}
}
}

private void finish(Result result) {
if (isCancelled()) result = null;
[color=red]onPostExecute[/color](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.另外新启动的要在后面排队。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值