• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 205
  • Last Modified:

Shutting down with Many Threads...

I have a multi-threaded app that I want to shut down but only after all threads have completed.
Is it OK to call System.exit(0); ?
Will this do what I want?
Thanks,
Slinky
0
slinky
Asked:
slinky
  • 2
  • 2
  • 2
  • +2
1 Solution
 
AlexJ030200Commented:
Hi slinky,

System.exit(0) will interrupt all of the non completed threads.

In order to do what you want, you should wait for all of the threads to die.

One simple way to achieve that is to create all of your threads in a new
group and while there are still some
threads alive in the group
(activeGroupCount() in class ThreadGroup), pick anyone (enumerate in
class ThreadGroup) and join it (join() in class thread).

Hope this helps.
0
 
heyhey_Commented:
> I want to shut down but only after all threads have completed.

well - the app will stop if all the (non-daemon) Threads have completed :)

0
 
heyhey_Commented:
> I want to shut down but only after all threads have completed.

well - the app will stop if all the (non-daemon) Threads have completed :)

0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
snjkrvaCommented:
Create a ThreadGroup and assign all the thread to it....
before System.exit(0) check whether threads are alive or not....

isAlive() method....for all the threads....u can also check for count...it should be one as main thread...


regards,
sanjeev
0
 
snjkrvaCommented:
Create a ThreadGroup and assign all the thread to it....
before System.exit(0) check whether threads are alive or not....

isAlive() method....for all the threads....u can also check for count...it should be one as main thread...


regards,
sanjeev
0
 
Jim CakalicSenior Developer/ArchitectCommented:
snjkrva, I don't see how your "answer" is fundamentally different than the comment made by AlexJ long before you joined the topic.
0
 
slinkyAuthor Commented:
I'll have to reject this as jim_cakalic is right...
0
 
slinkyAuthor Commented:
Cheers dude...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 2
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now