Solved

Shutting down with Many Threads...

Posted on 2000-05-10
8
189 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This video teaches viewers about errors in exception handling.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

777 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