File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes is that possible fro a thread ??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "is that possible fro a thread ???" Watch "is that possible fro a thread ???" New topic

is that possible fro a thread ???

abhay jain
Ranch Hand

Joined: Jun 03, 2011
Posts: 130
is that possible for an other thread to be alive after main() Thread dies ???
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46437
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15101

It doesn't even have to die in a dramatic way (for example because of an uncaught exception caused by a division by zero, as Campbell demonstrates). Other threads will remain running after the main thread ends, unless they are daemon threads. You can make a thread a daemon thread by calling setDaemon(true) on it.

Note the API documentation:
java.lang.Thread wrote:
When a Java Virtual Machine starts up, there is usually a single non-daemon thread (which typically calls the method named main of some designated class). The Java Virtual Machine continues to execute threads until either of the following occurs:

  • The exit method of class Runtime has been called and the security manager has permitted the exit operation to take place.
  • All threads that are not daemon threads have died, either by returning from the call to the run method or by throwing an exception that propagates beyond the run method.

  • Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
    Rob Spoor

    Joined: Oct 27, 2005
    Posts: 20279

    A JVM doesn't end until all non-daemon* threads have stopped. That means that if you launch another non-daemon thread, and that thread remains alive when the main thread ends, the JVM does not end yet. The Event Dispatcher Thread (EDT) of AWT/Swing is often such a thread. Threads created for java.util.Timer objects are also such threads. And of course, any thread without setDaemon(false) called on them are such threads.

    * All daemon threads will end when all non-daemon threads have ended as well. Check out Thread's setDaemon method for more info.

    SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
    How To Ask Questions How To Answer Questions
    I agree. Here's the link:
    subject: is that possible fro a thread ???
    It's not a secret anymore!