File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Thread states in Java 5 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 » Java » Threads and Synchronization
Bookmark "Thread states in Java 5" Watch "Thread states in Java 5" New topic
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: 2982
    
    9
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: 2982
    
    9
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: 2982
    
    9
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread states in Java 5
 
Similar Threads
Thread RUNNABLE vs RUNNING
methods which stop a thread?
GC
Thread
newly created thread state?