Skip to content

Instantly share code, notes, and snippets.

@daichan4649
Created January 25, 2012 04:17
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 daichan4649/1674675 to your computer and use it in GitHub Desktop.
Save daichan4649/1674675 to your computer and use it in GitHub Desktop.
The difference between Gingerbread and ICS of AsyncTask's Executor
// android.os.AsyncTask 抜粋
// Executor
private static final ThreadPoolExecutor sExecutor = new ThreadPoolExecutor(CORE_POOL_SIZE,
MAXIMUM_POOL_SIZE, KEEP_ALIVE, TimeUnit.SECONDS, sWorkQueue, sThreadFactory);
// execute
public final AsyncTask<Params, Progress, Result> execute(Params... params) {
// 抜粋
sExecutor.execute(mFuture);
return this;
}
// android.os.AsyncTask 抜粋
// Executor
public static final Executor SERIAL_EXECUTOR = new SerialExecutor();
private static volatile Executor sDefaultExecutor = SERIAL_EXECUTOR;
private static class SerialExecutor implements Executor {
final ArrayDeque<Runnable> mTasks = new ArrayDeque<Runnable>();
Runnable mActive;
public synchronized void execute(final Runnable r) {
mTasks.offer(new Runnable() {
public void run() {
try {
r.run();
} finally {
scheduleNext();
}
}
});
if (mActive == null) {
scheduleNext();
}
}
protected synchronized void scheduleNext() {
if ((mActive = mTasks.poll()) != null) {
THREAD_POOL_EXECUTOR.execute(mActive);
}
}
}
// execute
public final AsyncTask<Params, Progress, Result> execute(Params... params) {
return executeOnExecutor(sDefaultExecutor, params);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment