Skip to content

Instantly share code, notes, and snippets.

@curioustechizen
Created March 11, 2013 06:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save curioustechizen/5132347 to your computer and use it in GitHub Desktop.
Save curioustechizen/5132347 to your computer and use it in GitHub Desktop.
Snippet demonstrating how to ensure AsyncTasks are always executed in parallel (the default pre-HC behavior)
import android.annotation.TargetApi;
import android.os.AsyncTask;
import android.os.Build;
public class AsyncTaskUtils {
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static <P> void executeAsyncTaskInParallel(
AsyncTask<P, ?, ?> asyncTask, P... params) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);
} else {
asyncTask.execute(params);
}
}
}
//Instead of myAsyncTaskInstance.execute(params), use the following:
AsyncTaskUtils.executeAsyncTaskInParallel(myAsyncTaskInstance, params);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment