Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread RUNNABLE vs RUNNING Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread RUNNABLE vs RUNNING" Watch "Thread RUNNABLE vs RUNNING" New topic
Author

Thread RUNNABLE vs RUNNING

pedro abs
Ranch Hand

Joined: Mar 25, 2010
Posts: 33
Given this example:




Output:
Recently created: NEW
RUNNABLE

inside the run method of class DoIt, the message should not be returned RUNNING instead of RUNNABLE since at the time of the call "System.out.println(Thread.currentThread().getState());" happens, the thread in question has to be running and not just eligible to run?
Minhaj Mehmood
Ranch Hand

Joined: Jan 22, 2007
Posts: 400

pedro abs wrote:Given this example:




Output:
Recently created: NEW
RUNNABLE

inside the run method of class DoIt, the message should not be returned RUNNING instead of RUNNABLE since at the time of the call "System.out.println(Thread.currentThread().getState());" happens, the thread in question has to be running and not just eligible to run?


There is no such "RUNNING" state in java.lang.Thread.State enum class, However RUNNABLE indicates
http://download.oracle.com/javase/6/docs/api/java/lang/Thread.State.html#RUNNABLE wrote:A thread in the runnable state is executing in the Java virtual machine but it may be waiting for other resources from the operating system such as processor.


Hope this helps

Minhaj


SCJP6 96% | SCWCD5 81% | SCDJWS5 79%
Sudhakar Sharma
Ranch Hand

Joined: Apr 04, 2009
Posts: 71

Hi abs,

The API states:

public static enum Thread.State
extends Enum<Thread.State>

A thread state. A thread can be in one of the following states:

NEW
A thread that has not yet started is in this state.
RUNNABLE
A thread executing in the Java virtual machine is in this state.
BLOCKED
A thread that is blocked waiting for a monitor lock is in this state.
WAITING
A thread that is waiting indefinitely for another thread to perform a particular action is in this state.
TIMED_WAITING
A thread that is waiting for another thread to perform an action for up to a specified waiting time is in this state.
TERMINATED
A thread that has exited is in this state.
A thread can be in only one state at a given point in time. These states are virtual machine states which do not reflect any operating system thread states.


I think it is unable to determine whether it is RUNNABLE or RUNNING, because the context-switching is operating system dependent, therefore there is no such constant named RUNNING, which is why Thread.yield() method have not guaranteed behavior.

Hello Ranchers please confirm

Thank You
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Thread RUNNABLE vs RUNNING