Created
January 25, 2012 04:17
-
-
Save daichan4649/1674675 to your computer and use it in GitHub Desktop.
The difference between Gingerbread and ICS of AsyncTask's Executor
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
// 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; | |
} |
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
// 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