?
Solved

Shutting down with Many Threads...

Posted on 2000-05-10
8
Medium Priority
?
201 Views
Last Modified: 2010-04-01
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
Comment
Question by:slinky
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 2

Accepted Solution

by:
AlexJ030200 earned 150 total points
ID: 2796630
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
 
LVL 16

Expert Comment

by:heyhey_
ID: 2796964
> 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
 
LVL 16

Expert Comment

by:heyhey_
ID: 2796993
> 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:snjkrva
ID: 2798250
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
 

Expert Comment

by:snjkrva
ID: 2798251
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
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 2798635
snjkrva, I don't see how your "answer" is fundamentally different than the comment made by AlexJ long before you joined the topic.
0
 
LVL 1

Author Comment

by:slinky
ID: 2837873
I'll have to reject this as jim_cakalic is right...
0
 
LVL 1

Author Comment

by:slinky
ID: 2837876
Cheers dude...
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month16 days, 15 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question