Skip to content

Instantly share code, notes, and snippets.

@kmansoft
Created April 20, 2012 20:01
Show Gist options
  • Save kmansoft/2431415 to your computer and use it in GitHub Desktop.
Save kmansoft/2431415 to your computer and use it in GitHub Desktop.
static class TestAsyncTask extends AsyncTask<Void, Void, Void> {
private static final String TAG = "TestAsyncTask";
private static final int TASK_COUNT = 5;
@Override
protected Void doInBackground(Void... params) {
Log.i(TAG, "doInBackground " + mId);
try {
Thread.sleep(5 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
mLatch.countDown();
return null;
}
@Override
protected void onPreExecute() {
Log.i(TAG, "onPreExecute " + mId);
}
private TestAsyncTask(int id, CountDownLatch latch) {
mId = id;
mLatch = latch;
}
private int mId;
private CountDownLatch mLatch;
public static void testDefaultExecutor() {
Log.i(TAG, "testDefaultExecutor");
CountDownLatch latch = new CountDownLatch(TASK_COUNT);
for (int i = 0; i < TASK_COUNT; ++i) {
TestAsyncTask task = new TestAsyncTask(i, latch);
task.execute();
}
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void testThreadPoolExecutor() {
Log.i(TAG, "testThreadPoolExecutor");
CountDownLatch latch = new CountDownLatch(TASK_COUNT);
for (int i = 0; i < TASK_COUNT; ++i) {
TestAsyncTask task = new TestAsyncTask(i, latch);
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment