Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread State

 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well till today i was under the impression that Threads have a state called "Running". A thread is in "Running" state when its being currently executed by the JVM.

In case if the currently executing thread invokes yield() or if some high priority thread comes in the current thread moves to a state called "Runnable" and waits for its turn from the scheduler.

This is what the explanation is there in most of the books.

I saw the Thread API which has Thread.State:
The states are

BLOCKED
Thread state for a thread blocked waiting for a monitor lock.
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

1) There is no state like "Running"
2) Have a look at the explanation of "Runnable":A thread executing in the Java virtual machine is in this state.

Furher the code, proves the very same:


Result:
Thread Name:WorkerThread
I am run method
WorkerThread state is:RUNNABLE


a) This shows there is no state called "Running".
b) Is there a state called "xxxx" which is like " am waiting for my turn for the scheduler" like the so-called "Runnable" state described in books.
[ December 08, 2008: Message edited by: Deepak Jain ]
 
nav katoch
Ranch Hand
Posts: 246
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Deepak for a new stuff to learn. Nice explanation with example.

-Naveen
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good Work Deepak
 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone please answer this question.
 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the book incorrrect about "Running" state.
I have seen in K&B book , the Threads chapter exclusively talks about "Running" state?

Can someone explain this?
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I think there IS a running state. A simple proof of this thing is the yield method (as you also mentioned) which moves a thread from running state to runnable state. The only thing is that you cannot tell when a thread will be running and when it will be runnable. So I feel that when you call start on a thread, and the thread has not finished, in that time you will have to call it runnable (as opposed to running) as you don't whether it has CPU time or not...
 
Mukherji Sandeep
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Deepak,
I think when you run the program you get the final result on your screen and not the exact state changes. When the thread is in the running process it is not being printed. see you didn't get the state as NEW also.
 
Brian Legg
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very interesting, I'd like to hear more about this as well. Mostly just posting to let you know that you listed BLOCKED twice though.
 
Himanshu Gupta
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Desperately waiting for some nice explanation..
 
Alain Dickson
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

Was just passing by, Since threads is a topic which interests me I started looking into this.

In J2SE 1.6 documentation, Don't just look at the start of documentation for this Enum type. If you go to see the details it says the following, which is self explainatory.

RUNNABLE
public static final Thread.State RUNNABLE
Thread state for a runnable thread.
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.

AS FAR AS BOOKS ARE CONCERNED: They do defrentiate between running and runnable state to just give a deeper understanding but they should not call "RUNNING" as a state.

Reason Being: Even if we suppose there is a state called running it cannot be determined like other states. As the documentation and books say that this behaviour of the scheduler cannot be guranteed(i.e. it is not dependent on our program) where as other states(NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WATING, TERMINATED) are determined by our program.

Two states RUNNABLE and RUNNING (Back and forth by the scheduler) is not determined by our program.
 
Brian Legg
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Two states RUNNABLE and RUNNING (Back and forth by the scheduler) is not determined by our program. " And thus the program does not differ the 2.

Thank you Alain Dickson for a very great post of information!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic