jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread life cycle Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread life cycle" Watch "Thread life cycle" New topic
Author

Thread life cycle

Swamy Nathan
Ranch Hand

Joined: May 16, 2004
Posts: 187
I have been staring at these 3 diagrams on thread lifecycle and I have some questions.

diagram 1 is online as

for diagram 2 please open Khalid's book page 367 figure 9.3
for diagram 3 see k&B page 507 figure 9.2.

I would like to discuss them thread bare. Do you think it is safe to upload the 2 images 1 from Khalid's book on the thread lifecycle and the one from the K&B book on same topic purely to do a discussion. Would the authors or publishers object?

I have the images ready with me. If you guys (Java Ranch Big Moose) say its okay I will put them up somewheer in geocities and bring those images into this forum for the discussion. If no one objects I will start the discussion tomorrow IST 11:00. Otherwise will start without the images online.

Thanks.
[ June 22, 2004: Message edited by: Swamy Nathan ]
Pal Sudarshan
Ranch Hand

Joined: Jun 10, 2004
Posts: 52
Until the Big Moose says yes, can we discuss your questions. I am also at the same stage and would like to have some discussion on the topic.
Baps Vakkalagadda
Ranch Hand

Joined: May 25, 2004
Posts: 47
I do not know whether it is allowed or not. but it is very easy to create them in Paint (Save them in JPEG or GIF format).
Swamy Nathan
Ranch Hand

Joined: May 16, 2004
Posts: 187
I hope you all and especially Corey have the books

Q1. Is runnable same as "ready to run"?
The diagram1 from theJava Tutorial gives the impression to me that "running" is not a state. It seems to be the just a stage after the start() method is invoked and the run() method has been called. A running thread is running till the run method returns.
This differes from what is depicted in diagram2 and diagram3.
I felt that running should not be a state. It should be just the stage after start when run() has started and has not returned. This way one can visualise that while a thread is "running" at some point it may be "non-runnable" because it is blocked(for join or I/O or lock) or waiting(for notification) or sleeping, at some point it may be excuting and at some point it is just frozen because some other thread is currently executing. Is there such a state when a thread is running but frozen( not blocked, waiting or sleeping and still not executing becuase some other thread is currently executing)
Q2)What do you think?
Based on the reply I will ask a few more questions.
Thanks.
[ June 23, 2004: Message edited by: Swamy Nathan ]
Swamy Nathan
Ranch Hand

Joined: May 16, 2004
Posts: 187
This is largely about using technical names for what is otherwise understandable using simple logic. Based on the small writeup and the vague image in the java tutorial I think I got confused.

If we completely forget the java tutorial (they did say they were giving a simplistic explanation) it might make sense to say that

After start() when run() begins, a thread becomes "runnable"(or "ready-to-run") and not "running". A runnable thread if it actually executes on the CPU is "running". It then becomes the current thread. Only "running" and hence current threads can be put to sleep, made to wait or block. Now both diagram2 and diagram3 make complete sense and diagram1 should be ignored.
[ June 23, 2004: Message edited by: Swamy Nathan ]
Swamy Nathan
Ranch Hand

Joined: May 16, 2004
Posts: 187
Now that that is done with heres another question.

Q) If we have 5 CPU's it is possible that more than one thread be running concurrently. What is returned by "public static Thread currentThread()"

Also if we call "Thread.sleep(x)" which thread is put to sleep?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
The second question is easy. Thread.sleep(long) puts the calling thread to sleep.

For the first question gotta research into it


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Thread.currentThread() returns a reference to the calling thread. Each thread executing on each of the CPUs gets a reference to its OWN thread.
[ June 23, 2004: Message edited by: Barry Gaunt ]
Swamy Nathan
Ranch Hand

Joined: May 16, 2004
Posts: 187
Cool. I was surprised when u said research since one answer lead to the other.

Well I gues Sun should improve the API by giving more intuitive method names to these methods- like "callingThread" instead of "currentThread" which can mean many things.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Thread life cycle