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.另外新启动的要在后面排队。

更多相关文章

  1. 添加广告的时候学到的东西
  2. 翻翻git之---实用工具类Lazy(绝对的好东西,走过路过别错过)
  3. NDK模块开发:关于音视频,你需要了解的东西是什么?
  4. 火爆新东西,仿QQ版本的ResideMenuItem框架(最新QQ版本的)
  5. unity3d + android 好东西。。。
  6. React Native常用组件轮子(适配Android优先,持续更新中)
  7. 【移动开发】Android中一些你可能不太知道的东西
  8. 【干货推荐】Android市场今非昔比,Android开发者该学习哪些东西提
  9. 【原版的:参赛作品】窥秘懒---android打开下拉手势刷新时代

随机推荐

  1. android:maxLines和android:ellipsize同
  2. Android开发资源整理
  3. Android 数据存储与读取:SQLite
  4. Android学习——AndroidX
  5. Android:Nothing to show in Android Pro
  6. Edittext android:inputType 输入的限制
  7. Android中Handler引起的内存泄露
  8. android图片压缩质量参数Bitmap.Config R
  9. android sdk 升级出错及解决方案
  10. Android 远程调试 JNI 实现