当前位置: 首页 > 面试题库 >

等待多个AsyncTask完成

张承颜
2023-03-14
问题内容

我通过将操作拆分为可用的确切内核数来并行化操作,然后通过启动相同数量的AsyncTask,对数据的不同部分执行相同的操作。

我正在使用executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, ...)以并行化它们的执行。

我想知道每个线程何时完成其工作,以便结合所有结果并执行进一步的操作。

我能怎么做?


问题答案:

您还可以简单地将共享库中的计数器递减作为的一部分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"));    
        }
    }
}


 类似资料:
  • 根据Espresso文档,检测测试应该自动等待完成。但它不起作用。我创建了这个简单的测试用例: 返回UI线程时测试应该失败,但它总是成功。这是测试的logcat输出: 正如您所看到的,测试在后台方法执行之前就已经完成了。我怎样才能让考试等待呢?

  • 问题内容: 我有一个循环,可以调用API并将结果编译成数组。我如何等待所有调用完成后才能恢复执行?我看到了一系列有关如何等到打完一个电话的答案,但我不知道如何检查所有这些。如果我做一个while循环,一直等到’obj’是正确的长度,则页面只会停顿直到调用完成,这不是我想要的。请帮助? 问题答案: 如果您使用jQuery的deferred,这很容易。有一种方法,等待多个诺言完成,然后运行回调。那就是

  • 我正在运行这样的多个服务:(例如,在多个线程中读取文件) 是一个扩展类的类,它正在做一些事情,比如读取文件。它是从另一个线程调用的,该线程不是JavaFX应用程序线程。 我如何等到所有这些服务完成后再调用? 可复制的例子:

  • 问题内容: 在我的程序执行过程中,启动了多个线程。线程数量取决于用户定义的设置,但是它们都使用不同的变量执行相同的方法。 在某些情况下,需要在执行过程中进行清理,其中一部分是停止所有线程,尽管我不希望它们立即停止,我只是设置了一个变量来检查它们是否终止。问题在于线程停止之前最多可能需要1/2秒。但是,我需要确保所有线程都已停止,然后才能继续进行清理。清理是从另一个线程执行的,因此从技术上讲,我需要

  • 问题内容: 有什么方法可以简单地等待所有线程处理完成?例如,假设我有: 如何更改此方法,以便该方法在注释处暂停直到所有线程的方法退出?谢谢! 问题答案: 你将所有线程放入数组中,全部启动,然后进行循环 每个连接将阻塞,直到相应的线程完成为止。线程的完成顺序可能不同于你加入线程的顺序,但这不是问题:退出循环时,所有线程均已完成。

  • 问题内容: 我有一个用ngResource定义的工厂: 我正在对该工厂定义的查询方法进行多次调用。这些调用可以异步发生,但是我需要等待两个调用完成才能继续: 有没有办法用ngResource定义的AngularJS工厂做到这一点,类似于jQuery的$ .when()。then()功能?我不想将jQuery添加到当前项目中。 问题答案: 您将要使用promise和$ q.all()。 基本上,您可