等待多个 AsyncTask 完成

Wait for multiple AsyncTask to complete(等待多个 AsyncTask 完成)

本文介绍了等待多个 AsyncTask 完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am parallelizing my operation by splitting it in the exact number of cores available and then, by start the same number of AsyncTask, performing the same operation but on different portions of data.

我正在使用 executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, ...) 来并行执行它们.

I am using executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, ...) in order to parallelize the execution of them.

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

I would like to know when every thread finishes its job so that combine all results and perform further operations.

我该怎么办?

推荐答案

作为 onPostExecute 的一部分,您还可以简单地减少共享对象中的计数器.由于 onPostExecute 运行在同一个线程(主线程)上,因此您不必担心同步问题.

You could also simply decrement a counter in a shared object as part of onPostExecute. As onPostExecute runs on the same thread (the main thread), you won't have to worry about synchronization.

更新 1

共享对象可能如下所示:

The shared object could look something like this:

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 正在寻找一种解决方案,在该解决方案中他可以避免构建新类.这可以通过在生成的 AsyncTasks:

According to the comments for this answer, OP is looking for a solution in which he can avoid building a new class. This can be done by sharing an AtomicInteger among the spawned 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"));    
        }
    }
}

这篇关于等待多个 AsyncTask 完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:等待多个 AsyncTask 完成

基础教程推荐