aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread State Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread State" Watch "Thread State" New topic
Author

Thread State

Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
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 ]

SCJP, SCWCD, SCBCD
nav katoch
Ranch Hand

Joined: May 02, 2008
Posts: 246

Thanks Deepak for a new stuff to learn. Nice explanation with example.

-Naveen
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Good Work Deepak


SCJP 6
Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
Can someone please answer this question.
Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
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

Joined: Aug 03, 2008
Posts: 9321
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Mukherji Sandeep
Greenhorn

Joined: Dec 09, 2008
Posts: 6
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.


Mukherji Sandeep<br />Trying Hard to learn java
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
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.


SCJA
~Currently preparing for SCJP6
Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

Desperately waiting for some nice explanation..


My Blog SCJP 5 SCWCD 5
Alain Dickson
Ranch Hand

Joined: Dec 08, 2008
Posts: 53
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

Joined: Nov 07, 2008
Posts: 488
"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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread State