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 ]
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...
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.
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.
Joined: Nov 07, 2008
"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!