我的问题是我有两个异步任务执行一种矩阵乘法。因此,这两个任务都在访问同一个矩阵。一个在上部,另一个在下部。对于内存节省问题,我使用 ArrayLists 并删除不再需要的条目。问题是在这两个任务中都有 for 循环运行,在这个循环结束时它应该等待另一个任务。但我不知道该怎么做。 任务 1:
protected Void doInBackground(Void... paramArrayOfParams) {
android.os.Debug.waitForDebugger();
for(j=1; j<(size+1); j++)
{
....
try{ othertask.wait();
}catch(InterruptedException e){}
//wait for other task();
}
任务 2:
protected Void doInBackground(Void... paramArrayOfParams) {
android.os.Debug.waitForDebugger();
for(j=1; j<(size+1); j++)
{
....
notifyAll();
//notifythatroundisfinished();
}
我尝试使用通知和等待,但似乎这并不能解决问题。我不知道可以用来解决问题的任何其他方法。是否真的可以在两个都在运行时等待另一个任务?
最佳答案
我相信以下问题的答案可以为您提供有关如何管理多个异步任务的抽象信息。
How to manage multiple Async Tasks efficiently in Android
Running multiple AsyncTasks at the same time -- not possible?
和github例子:
https://github.com/vitkhudenko/test_asynctask (致谢:vitkhudenko)
关于java - 如何同步两个异步任务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18914014/