Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

main thread

 
Damien Howard
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 456
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic