Skip to content

Instantly share code, notes, and snippets.

@swy680614
Created July 24, 2016 08:25
Show Gist options
  • Save swy680614/34bdc3c9a9ac1719d6a81eafdbbb7df9 to your computer and use it in GitHub Desktop.
Save swy680614/34bdc3c9a9ac1719d6a81eafdbbb7df9 to your computer and use it in GitHub Desktop.
asynctask wait all finish
CountDownLatch cdl = new CountDownLatch(2);
@Override
protected void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new TestTask(cdl).execute();
new TestTask(cdl).execute();
cdl.await();
Log.i("vin","stop");
} catch (Exception e) {
Log.i("vin", e.toString());
}
}
private class TestTask extends AsyncTask<Void,Void,Void> {
private CountDownLatch mCdl;
public TestTask(CountDownLatch cdl) {
super();
mCdl = cdl;
}
@Override
protected Void doInBackground(Void... voids) {
try {
Thread.sleep(10);
} catch (Exception e) {
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
mCdl.countDown();
Log.i("vin", "onPostExecute");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment