Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Main thread finishes execution before child thread

 
Rituraj Jain
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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"
test.jpg
[Thumbnail for test.jpg]
 
Chinna Eranna
Ranch Hand
Posts: 174
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, Java Run time doesn't hangs. May be that was a mistake or I guess very old JVM might have that bug.
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rituraj Jain
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
azuki ooh,
Your post was moved to a new topic.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic