Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads

 
poorna k
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read in one of the faqs that the below stmt is false:
The JVM runs till the main method exits, even if there are other user threads running.
isnt it true that the program terminates when the main thread dies even tho' other user threads may be executing?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13056
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "main thread" is just the Thread used to execute the main method. It can easily start other threads and then die.
A Java application runs until either:
1) all user Threads are finished
2) The System.exit() method is called.
There is nothing magic about the "main" thread.
Bill
 
poorna k
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
poorna
 
Lahcen Mannou
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A VM exits also if all the remaining threads are DEAMON threads.
Ex. Say you created 2 user threads in your main() method, thread1 and thread2 and thread1 is a Deamon thread and thread 2 is not.
If the main thread finished (end of main() method), the VM continues to run.
Now if thread2 stopps the VM stopps immediatly without waiting for thread1 to stop (because it is a Deamon thread).
Hope I'm right.
Lahcen.
Originally posted by William Brogden:
The "main thread" is just the Thread used to execute the main method. It can easily start other threads and then die.
A Java application runs until either:
1) all user Threads are finished
2) The System.exit() method is called.
There is nothing magic about the "main" thread.
Bill


[This message has been edited by Lahcen Mannou (edited October 16, 2000).]
 
mohit joshi
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

However if your main thread is destroyed, say by assigning it to null, then the JVM exits. How ever if you set other self created threads to null, the remaining threads keep running.Say you create a thread A and within A you create another thread B and then set A to null, the B thread will Keep running.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic