问题描述
Android 2.3 之后真正的 Android 团队在 Async 任务中做了什么改变.当我执行以下代码时,我在 Android 2.3 和 3.0 中得到相同的结果.
What change did really Android team make in Async task after android 2.3. When I executed the following code I am getting same result in both Android 2.3 and 3.0.
package com.sample.asynctask;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
public class AsyncTaskTestActivity extends Activity {
private static final String TAG = "AsyncTaskTestActivity";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//ExecutorService executorService = Executors.newFixedThreadPool(1);
for (int i = 1; i <= 20; i++) {
TestTask testTask = new TestTask(i);
testTask.execute();
}
}
private static class TestTask extends AsyncTask<Void, Integer, Void> {
int i;
public TestTask(int i) {
Log.i(TAG, "Constructor for " + i);
this.i = i;
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
Log.i(TAG, "onPreExecute for " + i);
}
@Override
protected Void doInBackground(Void... params) {
Log.i(TAG, i + " Thread goes to sleep");
try {
Thread.sleep(20000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.i(TAG, i + " Thread wakes up");
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
Log.i(TAG, "onPostExecute for " + i);
}
}
}
我在 Gingerbread 中的假设:一次在一个线程池中执行 5 个异步任务.我在 Honeycomb 中的假设:1 个异步任务一次在一个线程池中执行.就像并发执行一样.
My assumption in Gingerbread: 5 Async task executes at one thread pool at a time.My assumption in Honeycomb: 1 Async task executes at one thread pool at a time. Exactly like concurrent execution.
但是,Gingerbread 和 Honeycomb 同时执行 5 个异步任务.
But, both Gingerbread and Honeycomb executes 5 Async tasks at the same time.
而且当异步任务的数量增加到 140 时,我没有收到 java.util.concurrent.RejectedExecutionException
.
And also when the number for Async task is increased to 140.i am not getting java.util.concurrent.RejectedExecutionException
.
我的假设是否正确?里面到底发生了什么?
Whether my assumptions are correct? What is really happening inside there?
推荐答案
我的假设是否正确?
你的假设是正确的,嗯,有点.
Whether my assumptions are correct?
Your assumptions is correct, well, sort of.
... ...
private static volatile Executor sDefaultExecutor = SERIAL_EXECUTOR;
... ...
... ...
// If the app is Honeycomb MR1 or earlier, switch its AsyncTask
// implementation to use the pool executor. Normally, we use the
// serialized executor as the default. This has to happen in the
// main thread so the main looper is set right.
if (data.appInfo.targetSdkVersion <= android.os.Build.VERSION_CODES.HONEYCOMB_MR1) {
AsyncTask.setDefaultExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
... ...
Android Gingerbread 之后 Async Task 真正发生了什么变化?
查看 AsyncTask 更改历史,更具体地说,这个:
2011 年 3 月 17 日 - AsyncTask 现在对应用程序 1 和 MR 使用 poll 执行器……
这是任务总数和每个任务的执行时间的一个因素,在另一个世界中,任务总数为140(比128更大),但是,线程池在任何给定的线程中分配的线程总数time 小于 128,换句话说,总是有一些空闲线程(由于最后一个任务完成并释放资源)在您的情况下可用.您可以尝试增加每个任务的执行时间,例如 Thread.sleep(10000)
,这可能会给您 RejectedExecutionException.
This is a factor of total number of tasks and execution time per each task, in another world, the total task number is 140 (which is bigger that 128), however, the total number of thread allocated by threadpool at any given time is smaller than 128, in another word, there are always some idle threads (due to last task finished and release resource) available in your case. you can try increase the execution time per each task for example Thread.sleep(10000)
, this will probably give you RejectedExecutionException.
这篇关于Android Gingerbread 之后 Async Task 真正发生了什么变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!