1

I have the following code in a command line application. Once the loop completes, my app is still running. Why is it not shutting down. From the logs, I can see that endIndex has reached. But the app is still running?

Executor exec = Executors.newFixedThreadPool(4);
for (int i = startIndex; i <= endIndex; i++) {
        final String spURL = urlPart + i;

        Runnable requestHandler = new Runnable() {
            @Override
            public void run() {
                try {
                    getImageForURL(spURL, 0);
                } catch (IOException ex) {
                } catch (Exception ex) {
                }
            }
        };
        exec.execute(requestHandler);
    }
1
  • Can you make sure that the function getImageForURL returns every time ? Commented Nov 17, 2011 at 8:13

2 Answers 2

3

Try this

ExecutorService e = Executors.newFixedThreadPool(4);
...
e.shutdown();
Sign up to request clarification or add additional context in comments.

Comments

0

You need to shutdown the executors after the loop (ideally in a finally block):
exec.shutdown() or exec.shutdownNow()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.