问题描述
也真的Android团队做出了Android 2.3之后异步任务有什么变化。当我执行以下code我得到两个的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);
}
}
}
我的姜饼假设:5异步任务一次一个线程池执行。我在蜂窝假设: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.
但是,既姜饼和蜂窝同时执行5异步任务。
But, both Gingerbread and Honeycomb executes 5 Async tasks at the same time.
和还当了异步任务数增加到140.i我没有得到 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.
在 android.os.AsyncTask :
... ...
private static volatile Executor sDefaultExecutor = SERIAL_EXECUTOR;
... ...
已 android.app.ActivityThread :
... ...
// 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的姜饼后也确实发生在异步任务怎样的变化?
查看AsyncTask的更改历史,更确切地说,这一个:
What change did really happen in Async Task after Android Gingerbread?
Check out AsyncTask change history, more specifically, this one:
2011年3月17日 - AsyncTask的现在使用的民调执行程序的应用程序多达通过慧聪MR1和T ...
这是任务和每每个任务执行时间的总数的一个因素,在另一个世界,总任务数140(这是更大的那个128),然而,给定的线程在任何分配的线程池的总数时间小于128,在另外一个词,总有一些空闲线程(由于最后一个任务完成并释放资源)可以在你的情况。你可以尝试增加执行时间按每个任务,例如视频下载(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的姜饼后也确实发生在异步任务怎样的改变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!