Skip to content

Instantly share code, notes, and snippets.

@JakeWharton
Created June 15, 2012 05:24
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save JakeWharton/2934815 to your computer and use it in GitHub Desktop.
Save JakeWharton/2934815 to your computer and use it in GitHub Desktop.
Thread pool AsyncTask
/**
* Execute an {@link AsyncTask} on a thread pool.
*
* @param task Task to execute.
* @param args Optional arguments to pass to {@link AsyncTask#execute(Object[])}.
* @param <T> Task argument type.
*/
public static <T> void execute(AsyncTask<T, ?, ?> task, T... args) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.DONUT) {
throw new UnsupportedOperationException("This class can only be used on API 4 and newer.");
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
task.execute(args);
} else {
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment