0

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.

Richard
  • 1,087
  • 18
  • 52
  • ffmpeg.execute is creating a new thread in the pool for each iteration and you seem to be running out of threads too. Do your calculation on an `AsyncTask` for example or use a Thread `Executor` to limit your pool, etc. It’s not really clear what you’re asking or … rather what you’ve actually tried. – Martin Marconcini Sep 11 '18 at 20:58
  • @MartinMarconcini Well I tried showing what I am asking for by showing what I have done, Is it more understandable this way? – Richard Sep 11 '18 at 21:03

1 Answers1

2

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.

TheWanderer
  • 16,775
  • 6
  • 49
  • 63