Created
July 24, 2016 08:25
-
-
Save swy680614/34bdc3c9a9ac1719d6a81eafdbbb7df9 to your computer and use it in GitHub Desktop.
asynctask wait all finish
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
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