1. 多实例并行时,使用 asynchTask.execute()提交的任务是串行执行的.
下面分析原因:
以下是两个异步实例提交任务:
private void asynchTaskTest() {
// 异步任务1
TestAnsycTask testAnsycTask1 = new TestAnsycTask();
Log.e("TAG", "execute1 ThreadName:" + Thread.currentThread().getName());
testAnsycTask1.execute(new String[]{"111", "2222", "333"});
// 异步任务2
TestAnsycTask testAnsycTask2 = new TestAnsycTask();
Log.e("TAG", "execute2 ThreadName:" + Thread.currentThread().getName());
testAnsycTask2.execute(new String[]{"aaa", "bbbb", "ccc"});
}
private static class TestAnsycTask extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String[] params) {
Log.e("TestAnsycTask", "doInBackground threadName: " + Thread.currentThread().getName());
try {
Thread.sleep(1);

本文分析了Android中AsyncTask在多实例并行时默认的串行执行行为,解释了由于静态的SERIAL_EXECUTOR导致的任务顺序执行原因。同时,介绍了从Android 3.0开始,通过executeOnExecutor方法结合ASYNC_TASK_THREAD_POOL_EXECUTOR实现任务并行处理的方法。
392

被折叠的 条评论
为什么被折叠?



