| Author |
Threads, executors and ending a "hanging" main method
|
simon fletcher
Ranch Hand
Joined: Aug 04, 2012
Posts: 35
|
|
The following code is from a tutorial on multithreading (meant to introduce the CountDownLatch and incorporating a thread pool). When it's run, it just "hangs" at the end. If it wasn't using the thread pool it would use thread objects and the join() method could be called on each thread from the main thread and, presumably, the main thread would end when the other threads end. But what do you do to end the main thread when a thread pool is used and why does the main thread not just end after the last line of the main method anyway?
The ouptput is:
Started.
Started.
Started.
Completed.
...And cursor just sits at the end blinking and unresponsive.
Thanks.
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
|
|
You have an ExecutorService which is still available to process more Runnables or Callables. So it's waiting to be told to do something. That's why your application doesn't shut down.
(Hint: there's a way to tell an ExecutorService to shut down.)
|
 |
simon fletcher
Ranch Hand
Joined: Aug 04, 2012
Posts: 35
|
|
Got it. Thanks.
|
 |
 |
|
|
subject: Threads, executors and ending a "hanging" main method
|
|
|