问题
So I got stuck on a pretty basic Java thing.
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
}
});
}
}
When doing the normal fori-loop it does not always wait for all of the terminal calls to finish and just goes on.
回答1:
Have you considered recursion?
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++);
}
});
}
EDIT:
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.
来源:https://stackoverflow.com/questions/52284083/java-start-next-loop-iteration-after-inner-class-has-finished