Created
June 15, 2012 05:24
-
-
Save JakeWharton/2934815 to your computer and use it in GitHub Desktop.
Thread pool AsyncTask
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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