Meaningless Drivel is fun!
The moose likes Java in General and the fly likes When Main Thread Stops ? 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 » Java in General
Bookmark "When Main Thread Stops ?" Watch "When Main Thread Stops ?" New topic

When Main Thread Stops ?

Srinivasa Vallabha

Joined: Mar 08, 2001
Posts: 15
Hi all,
I got the following question and also the answer,
but I can't get the proper meaning out of this answer, can anybody please explain me, and if possible please give some coding example.
Q: 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?
Answer: Absolutely wrong!
The correct one:
When you start a program, 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 program spawns new threads from the main thread it does stop
until last thread id died.
Even if the main thread dies program keep running.
Thanks in advance
Srinivasa Vallabha
william kane
Ranch Hand

Joined: Nov 21, 2000
Posts: 260
public class TestMainThread{
public static void main(String args[]){
Thread tt=new ChildThread();
System.out.println("Main stopped");

class ChildThread extends Thread{
public void run(){
System.out.println("Sleep started");
}catch(Exception e){
The above is the example that reinstates the fact that the instance of the jvm exists until ALL THE NON DEAMON THREADS RETURN.
As u run the above example u can observe that even after the main thread (that started the child thread) is stopped the jvm continues to run.
ps donot use the stop method in development its depricated :-)

Help me!Help you!!!
I agree. Here's the link:
subject: When Main Thread Stops ?
It's not a secret anymore!