When does the main thread stop in Java?

23,682

Solution 1

The program terminates when all non-daemon threads die (a daemon thread is a thread marked with setDaemon(true); it's usually used for utility threads). From the documentation:

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.

Solution 2

I read this statement: “The main thread must be the last thread to finish execution. When the main thread stops, the program terminates.”Is it true?

No, it is not. The virtual machine terminates if the last non-daemon thread has finished. It doesn't have to be the main thread.

Simple example:

public static void main(String[] args) {

   System.out.println("Main thread started");
   new Thread(new Runnable() {
     @Override
     public void run() {
        System.out.println("Second thread started");
        try {
          Thread.sleep(2000);  // wait two seconds
        } catch(Exception e){}
        System.out.println("Second thread (almost) finished");
     }
   }).start();
   System.out.println("Main thread (almost) finished");
}

Solution 3

When the main thread stops, the program terminates.

The program terminates when there no longer is any non-daemon thread running (or someone called System.exit). The main thread can have finished long ago.

Solution 4

The JVM will exit when the main thread and all non-daemon threads finish execution.

When you create a new thread, you can call Thread.setDaemon(true) to make it a daemon thread. If you do this, then the JVM will not wait until this thread finishes before execution. This is useful for any threads you create which are made to run in the background until the program stops.

If you create a new thread and do not call Thread.setDaemon(true), then the JVM will delay exit until this thread is complete, even if the main thread is finished.

Solution 5

When the main thread was start it'll not wait for the another thread which was created by us until they if can't use the join() of the thread class to wait for this thread. So basically if the child thread or sub thread getting more time for processing the task and you don't use the join() then main thread may be stop. To keep with main thread you must use the join() so the main thread stop after only this related thread are stop

check this link

http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html#join%28%29

Share:
23,682
Saravanan
Author by

Saravanan

I am Saravanan MCA Graduate.I am working as a Senior Developer in java environment. Email:[email protected]

Updated on November 08, 2020

Comments

  • Saravanan
    Saravanan over 3 years

    I read this statement:

    The main thread must be the last thread to finish execution. When the main thread stops, the program terminates.

    Is it true?

    I also came to know "Even if the main thread dies, the program keeps running".

    This is my current understanding:

    • When you start a program, the JVM creates one thread to run your program.
    • The JVM creates one user thread for running a program. This thread is called main thread.
    • The main method of the class is called from the main thread.
    • If a program spawns new threads from the main thread, the program waits until the last thread dies.

    Which one is true?