Solved

Shutting down with Many Threads...

Posted on 2000-05-10
8
187 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 50 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
 

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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now