Forked from greenrobot/AsyncTaskExecutionHelper.java
Last active
August 29, 2015 14:21
-
-
Save evasyuk/c0faa3a26a224bfb9173 to your computer and use it in GitHub Desktop.
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
package de.greenrobot.util; | |
import java.util.concurrent.Executor; | |
import android.os.AsyncTask; | |
import android.os.Build; | |
/** | |
* Uses level 11 APIs when possible to use parallel/serial executors and falls back to standard execution if API level | |
* is below 11. | |
* | |
* @author Markus | |
*/ | |
public class AsyncTaskExecutionHelper { | |
static class HoneycombExecutionHelper { | |
public static <P> void execute(AsyncTask<P, ?, ?> asyncTask, boolean parallel, P... params) { | |
Executor executor = parallel ? AsyncTask.THREAD_POOL_EXECUTOR : AsyncTask.SERIAL_EXECUTOR; | |
asyncTask.executeOnExecutor(executor, params); | |
} | |
} | |
public static <P> void executeParallel(AsyncTask<P, ?, ?> asyncTask, P... params) { | |
execute(asyncTask, true, params); | |
} | |
public static <P> void executeSerial(AsyncTask<P, ?, ?> asyncTask, P... params) { | |
execute(asyncTask, false, params); | |
} | |
private static <P> void execute(AsyncTask<P, ?, ?> asyncTask, boolean parallel, P... params) { | |
if (Build.VERSION.SDK_INT >= 11) { | |
HoneycombExecutionHelper.execute(asyncTask, parallel, params); | |
} else { | |
asyncTask.execute(params); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment