您还可以简单地将共享库中的计数器递减作为的一部分
onPostExecute。由于
onPostExecute在同一线程(主线程)上运行,因此您不必担心同步。
更新1
共享对象可能看起来像这样:
public class WorkCounter { private int runningTasks; private final Context ctx; public WorkCounter(int numberOfTasks, Context ctx) { this.runningTasks = numberOfTasks; this.ctx = ctx; } // only call this in onPostExecute! (or add synchronized to method declaration) public void taskFinished() { if (--runningTasks == 0) { LocalBroadcastManager mgr = LocalBroadcastManager.getInstance(this.ctx); mgr.sendBroadcast(new Intent("all_tasks_have_finished")); } }}更新2
根据对此答案的评论,OP正在寻找一种可以避免建立新课程的解决方案。这可以通过
AtomicInteger在生成的
AsyncTasks中共享一个来完成:
// TODO Update type params according to your needs.public class MyAsyncTask extends AsyncTask<Void,Void,Void> { // This instance should be created before creating your async tasks. // Its start count should be equal to the number of async tasks that you will spawn. // It is important that the same AtomicInteger is supplied to all the spawned async tasks such that they share the same work counter. private final AtomicInteger workCounter; public MyAsyncTask(AtomicInteger workCounter) { this.workCounter = workCounter; } // TODO implement doInBackground @Override public void onPostExecute(Void result) { // Job is done, decrement the work counter. int tasksLeft = this.workCounter.decrementAndGet(); // If the count has reached zero, all async tasks have finished. if (tasksLeft == 0) { // Make activity aware by sending a broadcast. LocalBroadcastManager mgr = LocalBroadcastManager.getInstance(this.ctx); mgr.sendBroadcast(new Intent("all_tasks_have_finished")); } }}


