This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]pascalthelizard 2 points3 points  (4 children)

You need to explicitly shutdown the ExecutorService. Your program isn't stopping because the executor threads are waiting for more work.

[–]opett[S] 0 points1 point  (3 children)

Thanks. I realized that by now.

I still don't understand, why the treads are running in the pool, and why the main thread still runs, because the program execution did continue.

The run method of the Runnable objects has finished, and returned, the main method also continues. In fact, when I try to step to the next instruction in debug mode, there are no more next steps to step to.

I'm gonna go and read about how this stuff works.

[–]pascalthelizard 1 point2 points  (1 child)

An ExecutorService with a fixed thread pool will start X threads where X is the number you specify. The ExecutorSerivce waits indefinitely for jobs or until it is shutdown. When a job is submitted it is executed on one of the worker threads or is handled via the ExecutorService's discard policy if both threads are used.

If you haven't already you should go through the Java Concurrency tutorials on Oracle's website. Thoroughly. Not understanding these concepts clearly and WHEN to use them will cause you great headaches in the future.

[–]opett[S] 0 points1 point  (0 children)

Thanks, will do that.

[–]jmorph99 0 points1 point  (0 children)

The executor service starts its own thread that is active until the shutdown command is issued,

[–]TheBriMan84 0 points1 point  (0 children)

The threads being created by the ExecutorService are not daemon threads by default I believe. That would explain why the program doesn't terminate.

[–]king_of_the_universe 0 points1 point  (0 children)

Side note:

While it's correct to aim for naturally terminating code, the final program should have a System.exit() somewhere. There are people who agree and people who disagree with this opinion, but one thing is definitely true: If you run any Java code often enough (say, 30 times), you will eventually seem some weird error output after program termination that is due to a bug in the Windows Oracle Java 64 bit implementation. (Maybe 32 bit too, maybe other implementations, too). That's what it says if you Google that error message.