【发布时间】:2017-12-10 19:50:54
【问题描述】:
我正在通过onCreate 中的这个 sn-p 并行调用两个 AsyncTask:
if ((DictMode.equals("TATOEBA")) || (DictMode.equals("SENTENCE"))) {
dictAsyncTask = new DictAsyncTask(DictionaryLookUp.this, "Tatoeba");
// dictAsyncTask.execute();
dictAsyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
if ((DictMode.equals("JUKUU")) || (DictMode.equals("SENTENCE"))) {
dictAsyncTask = new DictAsyncTask(DictionaryLookUp.this, "Jukuu");
// dictAsyncTask.execute();
dictAsyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
在onPostExecute 期间,我检查是否有任何数据被检索并通过finish 从startActivityForResult 调用返回:
if (alldictStuff.size() == 0) {
...
Toast.makeText(DictionaryLookUp.this,"NOTHING FOUND ...",Toast.LENGTH_SHORT).show();
if ((DictMode.equals("TATOEBA")) || (DictMode.equals("JUKUU")) || (DictMode.equals("SENTENCE"))) {
Intent intentCard = new Intent(getApplicationContext(), CardView.class);
setResult(Activity.RESULT_OK);
finish();
}
但这显然是有缺陷的,因为我没有检查两个线程是否都没有返回结果,那么检查两个线程是否都已完成并且什么都不返回的最佳方法是什么?我是否必须使用自己的变量来跟踪它,或者有什么简洁/内置的东西我可以利用?
【问题讨论】: