Executing Multiple AsyncTask's Parallely
This is how I do that:
if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ) {
new MyAsyncTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
new MyAsyncTask().execute();
}
where MyAsyncTask
is regular AsyncTask subclass. Or you can wrap this all in helper class:
class TaskHelper {
public static <P, T extends AsyncTask<P, ?, ?>> void execute(T task) {
execute(task, (P[]) null);
}
@SuppressLint("NewApi")
public static <P, T extends AsyncTask<P, ?, ?>> void execute(T task, P... params) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);
} else {
task.execute(params);
}
}
}
and then just do:
TaskHelper.execute( new MyTask() );
or
TaskHelper.execute( new MyTask(), args );
or
TaskHelper.execute( new MyTask(constructorParams), args );