• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Threads, executors and ending a "hanging" main method

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it. Thanks.

 
reply
    Bookmark Topic Watch Topic
  • New Topic