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

Thread RUNNABLE vs RUNNING

 
pedro abs
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 400
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sudhakar Sharma
Ranch Hand
Posts: 71
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic