Win a copy of Pragmatic AI this week in the Artificial Intelligence forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

main()  RSS feed

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Can you please explain this statement preferable with an example code.

"When main() ends, that may or may not be the end of the program. The JVM will run until the only remaining threads are daemon threads. "
 
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

Is your question asking what a daemon thread is? Or is your question asking how main could spawn a separate thread? (Or both?)
 
Monica Dharwad
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot. I would like to know both.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Without multithreading, when a method is called, the next line of code does not execute until the method returns.

doThis(); //then when method returns...
doThis(); //then when method returns...
doThis(); //then when method returns...
finish();

But with multithreading, a separate process can start in its own thread, allowing the calling thread to continue on with what it's doing.

startNewThread(); //then as soon as start call returns...
finish();

In this case, even after finish() returns, the new thread's run method can continue executing separately. As long as the new thread is not a daemon, this would keep the program alive.

Basically a daemon thread is a background process whose completion is not required for the program to end.
 
Monica Dharwad
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for the reply. It explains the concept very well.
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When main() ends, that may or may not be the end of the program
i didnt get this line..
Because i think when main() ends it will surely terminate your program as it is main thread.
please clear my doubt

thank you.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Main is just a starting point of your program. Check out the following program:



Even after it prints out "End of main Method" the program will continue to run.
 
Rancher
Posts: 42975
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Because i think when main() ends it will surely terminate your program as it is main thread.



The program will terminate if all non-daemon threads have terminated. If a GUI is visible then there will be several non-daemon threads, e.g. the event handling thread. In many applications the main method terminates very quickly, right after constructing the GUI.
[ August 16, 2006: Message edited by: Ulf Dittmer ]
 
Nishita Jain
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello Vijay and Dittmer ,
thank you very much for your replies....i debug that code and cleared my doubt..
Nishita
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!