• 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

Do all running threads stop when JVM exits?

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do all running threads stop when you issue a command such as System.exit(0) and exit the JVM?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When the JVM exits, *all* threads running in the JVM will terminate.

Henry
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, if you System.exit(), then all threads (even native ones) will stop, because System.exit() blows away the process (in the JVMs that I know, anyway).

However, your Java program generally should not System.exit() unless it really has to. When you System.exit(), all threads stop, regardless of what they were doing at the time. Also, using System.exit() makes your code unsuitable for re-use in a Web applictation or other application server, because System.exit() will kill the whole process, not just the particular bit of the process that's running your particular application.

Instead of using System.exit(), you should arrange for all your non-daemon threads to exit cleanly. Perhaps you can use Thread.interrupt(). Perhaps you can have some sort of global shut-down flag that all threads check every now and then. Perhaps some of your threads should be daemons, so they do not delay JVM shut-down.

If your application uses graphics (Swing/AWT), then there is an extra issue. You need to make sure that none of your windows has the default parent. Instead, you should create your own dummy Frame as a parent for all of them. When it's time to shut down, you should dispose() that Frame. Then the application can shut down cleanly, without System.exit().
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, if it is the System.exit. Will it interrupt all running threads by calling thread.interrupt()?
 
Peter Chase
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, System.exit() just kills the whole process, which can be bad in any but the simplest application. That's why you should design your program to exit cleanly, without the use of System.exit().
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How a JVM implements its threads internally is an implementation detail and can vary from one JVM to another. When the JVM shuts down, it doesn't have to call any Java code, as the JVM itself is the thread manager (or simulator). Java Thread objects are mere abstractions of JVM threads.

...and JVM threads are generally abstractions of operating system threads, which are in turn abstractions of physical microprocessor threads.
reply
    Bookmark Topic Watch Topic
  • New Topic