This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes main thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "main thread" Watch "main thread" New topic
Author

main thread

Damien Howard
Ranch Hand

Joined: Apr 01, 2003
Posts: 456
If the main thread has finished executing all its code, but there are other non-daemon threads running, does the main thread still exist?
Is it still in the runnable pool? Could it be selected to run again by the thread scheduler even though it has no code left to execute?
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
I will only answer that which I can prove:
If the main thread has finished executing all its code, but there are other non-daemon threads running, does the main thread still exist?

t.isAlive() returns false. The Thread object still exists.

java.lang.ThreadGroup[name=main,maxpri=10]
Thread[main,5,main]
Thread[Thread-0,5,main]
Thread[Thread-A,5,main]
main: main thread isAlive=true
Thread-A: main thread isAlive= false
java.lang.ThreadGroup[name=main,maxpri=10]
Thread[Thread-0,5,main]
Thread[Thread-A,5,main]
[ July 13, 2003: Message edited by: Marlene Miller ]
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
I will only answer... or that which some expert says is so...
Is it still in the runnable pool? Could it be selected to run again by the thread scheduler even though it has no code left to execute?

My thread teacher is Doug Lea. In his book Concurrent Programming in Java, his state diagram has a transition from running to terminated labeled return, fail.
He also says a Thread terminates when its run method completes by either returning normally or throwing an unchecked exception.
Therefore, (in theory) returning from run() implies moving out of the runnable state.
[ July 13, 2003: Message edited by: Marlene Miller ]
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
I am trying to be really careful Damien because I know you will be taking the exam Monday. I don't want you to make a mistake on my account. I wish you the best.
Damien Howard
Ranch Hand

Joined: Apr 01, 2003
Posts: 456
Thanks. I'm not expecting something like this on the exam. It was more out of personal curiosity.
Thanks for wishing me luck, hopefully I won't need it, but I'm glad to have it all the same
 
wood burning stoves
 
subject: main thread
 
Similar Threads
wait() in synchronized code ??
JVM and Thread Help
Patrick Naughton and Threads
How is main a thread
Regarding Thread's join()