Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save johnkil/2935031 to your computer and use it in GitHub Desktop.
Save johnkil/2935031 to your computer and use it in GitHub Desktop.
Thread poll 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 <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