aspose file tools*
The moose likes Threads and Synchronization and the fly likes Main thread finishes execution before child thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Main thread finishes execution before child thread" Watch "Main thread finishes execution before child thread" New topic
Author

Main thread finishes execution before child thread

Rituraj Jain
Ranch Hand

Joined: Nov 28, 2008
Posts: 37

I have a question and needs an expert advice on this.

I have a main Method and i creates two child threads from there. Now my child threads are running and main thread has finished execution, is there is any possibility for my program to hang because of this reason.
I always had very clear concept that the child thread can run even after the main thread has finished running. but today i read in complete reference that


"in a multi-threaded program, the main thread must be the last
thread to finish running. If the main thread finishes before a child thread has completed,
then the Java run-time system may "hang"


[Thumbnail for test.jpg]



Rituraj Jain
Chinna Eranna
Ranch Hand

Joined: Dec 08, 2009
Posts: 174

No, Java Run time doesn't hangs. May be that was a mistake or I guess very old JVM might have that bug.


- Chinna
Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 416
    
    2

Your program will run until the last non daemon thread completes. Your main thread is obviously a non daemon thread but its normal to have others.

I'll assume you know what a daemon thread is if not google it or look at the Thread javadoc.


"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
Rituraj Jain
Ranch Hand

Joined: Nov 28, 2008
Posts: 37
Thanks guys for your replies. Ya i already know all these things but was just making sure that I am correct and the book isn't.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4174
    
  21

azuki ooh,
Your post was moved to a new topic.


Steve
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Main thread finishes execution before child thread