File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes main() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "main()" Watch "main()" New topic
Author

main()

Monica Dharwad
Greenhorn

Joined: Aug 14, 2006
Posts: 8
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. "
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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?)


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Monica Dharwad
Greenhorn

Joined: Aug 14, 2006
Posts: 8
Thanks a lot. I would like to know both.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Aug 14, 2006
Posts: 8
Thanks a lot for the reply. It explains the concept very well.
Nishita Jain
Ranch Hand

Joined: Mar 30, 2006
Posts: 97
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.


Nishita
Vijay Kiran
Greenhorn

Joined: Nov 18, 2004
Posts: 21
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.


Vijay<br />I blog, therefore I am<br /><a href="http://www.vijaykiran.com" target="_blank" rel="nofollow">www.VijayKiran.com</a>
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42634
    
  65
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 ]

Ping & DNS - my free Android networking tools app
Nishita Jain
Ranch Hand

Joined: Mar 30, 2006
Posts: 97
hello Vijay and Dittmer ,
thank you very much for your replies....i debug that code and cleared my doubt..
Nishita
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: main()