内部类完成后,Java启动Next循环迭代 [英] Java start Next loop iteration after inner class has finished
问题描述
所以我陷入了一个非常基本的Java问题上.
So I got stuck on a pretty basic Java thing.
也就是说,我有一个 iteration
,需要在 inner class
完成后转到下一个循环.但是由于内部类
需要花费大量时间,并且可以在内部类
中访问的变量
必须为 final
这会抛出 RejectedExecutionException
.
Namely I have an iteration
that needs to go to a next loop after the inner class
has finished. But since the inner class
takes a lot of time and the variables
that can be accessed in inner class
must be final
it throws me RejectedExecutionException
.
java.util.concurrent.RejectedExecutionException: Task android.os.AsyncTask$3@290074 rejected from java.util.concurrent.ThreadPoolExecutor@d50b99d[Running, pool size = 17, active threads = 17, queued tasks = 128, completed tasks = 42]
这是引发异常的简化解决方案.
Here is the simplified solution that threw the exception.
for (int i = 0; i < 10;i++) {
String[] cmd = { "-i", imageToBeFiltered.toString(), "-filter_complex", filters[loopCounter], imageWithFilter.toString()};
ffmpeg.execute(cmd, new ExecuteBinaryResponseHandler() {
@Override
public void onSuccess(String message) {
super.onSuccess(message);
//Continue loop only if it has reached onSuccess or onFailure
}
@Override
public void onFailure(String message) {
super.onFailure(message);
//Continue loop only if it has reached onSuccess or onFailure
}
});
}
}
在执行正常的 fori-loop
时,它并不总是等待所有 terminal调用
完成并继续进行.
When doing the normal fori-loop
it does not always wait for all of the terminal calls
to finish and just goes on.
推荐答案
您是否考虑过递归?
void myMethod(int index) {
String[] cmd = { "-i", imageToBeFiltered.toString(), "-filter_complex", filters[loopCounter], imageWithFilter.toString()};
ffmpeg.execute(cmd, new ExecuteBinaryResponseHandler() {
@Override
public void onSuccess(String message) {
super.onSuccess(message);
if (index < 10) myMethod(index++);
}
@Override
public void onFailure(String message) {
super.onFailure(message);
if (index < 10) myMethod(index++);
}
});
}
我猜想 loopCounter
应该是索引,在这种情况下,如果您使用此代码,则需要更改它.
I'm guessing that loopCounter
is supposed to be the index, in which case you'll want to change that if you use this code.
这篇关于内部类完成后,Java启动Next循环迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!