Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads, executors and ending a "hanging" main method

 
simon fletcher
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20955
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it. Thanks.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic