The normal way we do AsyncTask in Android is, from Android API:
private class DoIntenseTask extends AsyncTask<Object, Object, Void> {
protected Void doInBackground(Object... params) {
for (Object param : params) {
Object rtnObj = doIntenseJob(param);
publishProgress(rtnObj);
}
return null;
}
protected void onProgressUpdate(Object... progress) {
for (Object rtnObj : progress) {
updateActivityUI(rtnObj);
}
}
}
My intense tasks are loosely coupled and the execution order does not matter, by doing this way, a single thread is allocated to run a list of intense tasks. personally I think this is a sort of halfway solution. Yes, the intense job is not running in UI thread anymore, but still need execute one by one (in many cases, we are facing a list of intense job, I think this is also why the methods in AsyncTask are multi-parameterized). Google should make the API more reusable to solve different kind of scenario.
What I really like to have is run a number of doIntenseJob() in parallel managed by a threadpool (e.g. poolSize = 5). Looks like google do give a solution by AsyncTask.executeOnExecutor() but unfortunately only available since API level 11. I am developing app on mobile and wonder if there is a workaround that I can achieve the same behavior under API level 11.
Thanks in advance
Y
If your build target is set to API Level 11 or higher, and you want to specifically use parallel tasks, you will want to start stating that explicitly in your code, akin to:
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB) {
myTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void[]) null);
}
else {
myTask.execute((Void) null);
}
http://commonsware.com/blog/2012/04/20/asynctask-threading-regression-confirmed.html