• 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

When Main Thread Stops ?

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class TestMainThread{
public static void main(String args[]){
Thread tt=new ChildThread();
tt.start();
Thread.currentThread().stop();
System.out.println("Main stopped");

}
}
class ChildThread extends Thread{
public void run(){
try{
System.out.println("Sleep started");
Thread.sleep(50000);
System.out.println("Awakened");
}catch(Exception e){
e.printStackTrace();
}
}
}
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.
clear???
ps donot use the stop method in development its depricated :-)
kane
 
reply
    Bookmark Topic Watch Topic
  • New Topic