| Author |
Thread states in Java 5
|
Kalyan Anand
Ranch Hand
Joined: Feb 07, 2007
Posts: 194
|
|
As per K&B for Java 5 a thread can be in the following states New Runnable Running Waiting/blocked/sleeping Dead In the Java 5 API we have the following states * NEW * RUNNABLE * BLOCKED * WAITING * TIMED_WAITING * TERMINATED Given that the basic architecture of threads hasn't changed how do I compare these states with the states in K&B ? [ September 08, 2008: Message edited by: Santhosh Jali ]
|
 |
Mike Simmons
Ranch Hand
Joined: Mar 05, 2008
Posts: 2778
|
|
New = NEW Runnable or Running = RUNNABLE Blocked = BLOCKED Waiting (with no timeout) = WAITING Waiting (with timeout) or Sleeping = TIMED_WAITING Dead = TERMINATED
|
 |
kishore mang
Greenhorn
Joined: Nov 27, 2012
Posts: 10
|
|
Hi Mike,
I dont think Runnable and Running states are same as per your post.
Runnable = Thread.start() called
Running = run() method being executing
Regards
Kishore
|
 |
Mike Simmons
Ranch Hand
Joined: Mar 05, 2008
Posts: 2778
|
|
|
I didn't say they were the same. But both Runnable and Running map to Thread.State.RUNNABLE. RUNNABLE consists of everything Running plus everything that is Runnable.
|
 |
kishore mang
Greenhorn
Joined: Nov 27, 2012
Posts: 10
|
|
RUNNABLE consists of everything Running plus everything that is Runnable.
In most of the books it was mentioned as Runnable and Running are two different states, so cant be one part of the other.
Since When Thread.start() called CPU will schedule that thread but may not be running state, but when schedules and run() method called means
that thread is in running(using CPU and active) in conceptual manner. After sleeping() CPU will put in Runnable not compulsorily in Running(active).
So can we say now that a thread which is in Runnable is Running?
|
 |
Mike Simmons
Ranch Hand
Joined: Mar 05, 2008
Posts: 2778
|
|
kishore mang wrote:In most of the books it was mentioned as Runnable and Running are two different states, so cant be one part of the other.
Runnable and Running are two different states, but Thread.State.RUNNABLE is a single state that includes both Runnable and Running. Look at the documentation for Thread.State. What value could a Runnable thread possibly have, based on their definitions there? Annd what value could a Running thread have? For both, the only possible answer is RUNNABLE.
kishore mang wrote:So can we say now that a thread which is in Runnable is Running?
Not using Thread's getState() method, no, you can't. The getState() method does not know or care about the difference between Runnable and Running.
|
 |
kishore mang
Greenhorn
Joined: Nov 27, 2012
Posts: 10
|
|
|
Thanks Mike for your valuable explanation, now got your point.
|
 |
 |
|
|
subject: Thread states in Java 5
|
|
|