Wait for multiple AsyncTask to complete(等待多个 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 正在寻找一种解决方案,在该解决方案中他可以避免构建新类.这可以通过在生成的 AsyncTask
s:
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 AsyncTask
s:
// 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 完成
基础教程推荐
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 降序排序:Java Map 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01