Meaningless Drivel is fun!*
The moose likes Threads and Synchronization and the fly likes Threads, executors and ending a Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Threads, executors and ending a "hanging" main method" Watch "Threads, executors and ending a "hanging" main method" New topic
Author

Threads, executors and ending a "hanging" main method

simon fletcher
Ranch Hand

Joined: Aug 04, 2012
Posts: 50
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: 18541
    
    8

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: 50
Got it. Thanks.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads, executors and ending a "hanging" main method