Author Topic:   to maha - new state of a Thread ?
eram
ranch hand
posted March 15, 2000 09:02 AM             
Whats a new state of Thread ? Any example , with 2 or 3 lines of code. Thanks.

maha anna
bartender
posted March 15, 2000 09:47 AM             
The life cycle of a thread has foll. states
1. New state
When you just construct a Thread object, it is in the new state . It is like any other object. The special system resources meant for a thread are not yet allocated. Ex. Thread myT = new Thread();
2. Ready state  
This state is after you start the thread. Now the 'myT' thread is registered as a thread , in the theread scheduling mechanism. All system resources are allocated for this thread and it is in Ready state now.Ex. myT.start()
3. Blocked/waiting state  
This is when the thread is waiting for some system resource / sleep(..) / wait(..) method for ex. , is invoked on this thread.
4. Dead state  
This is is after run() method is completed. Once the thread is dead, it can not be restarted.

So, When the thread is in new state or in dead state the thread is not alive. So isAlive() method will return false
regds
maha anna

[This message has been edited by maha anna (edited March 15, 2000).]

Jim Yingst
sheriff
posted March 15, 2000 10:49 AM             
There seem to be several ways to list these - these are general categories, which can be broken down further in some cases. Bruce Eckel lists 4 states, replacing Ready with Runnable, which I think should really be split into Ready and Running as two distinct states (which is how Roberts/Heller/Ernest list them). RHE also list a number of separate states in place of Blocked/Waiting: Blocked, Asleep, Suspended, Waiting, and Seeking Lock. But RHE neglect to mention the New state - probably indicating that it won't be found on the exam, unlike the others.

maha anna
bartender
posted March 15, 2000 11:02 AM             
I started to explain Where the so called New state lies. In course of writing I thought I would mention only the broader/general catagories. Anyhow , I should have listed the Running state in between Ready statte and waiting state . Without going into Running state from Ready state how can a thread go to blocked state? May be I was sleeping while I was writing the above post.
Thank you Jim.
regds
maha anna

eram
ranch hand
posted March 15, 2000 11:11 AM             
Thanks Maha & Jim for ur replies.

questions:

Are these states mentioned in API or do they have different names for each state and each book comes up with each name for Thread states. I mean any Standards for thread states. I just need the link to JLS or API.

Can u put in the different state they move from their current state. for e.g Running to waiting etc...

maha anna
bartender
posted March 15, 2000 01:01 PM             
The below info explains the possible Thread state transfers and the reasoning for the same.

New State
- Ready State ( due to myT.start() )

Ready State
- Running State ( when picked up by scheduler for running )

Running State
- Ready State ( due to myT.yield() / Thread.yield() / Thread.currentThread().yield() )
- Blocked/Waiting State ( due to wait(..)/sleep(..)/waiting for something to happen )
- Dead state (it finished its job. (ie) run() completed /stop() is called
on this thread which anyway is deprecated and not good method to kill the thread )

Blocked/Waiting State
- Ready State ( when .interrupt() is invoked on this thread / it finishes it's sleep /
it wakes up from wait() / It got the object's lock it was waiting for/
IO data got it was waiting for etc.. )

Dead State
- Nothing
It would have been better if we explain the above info through a state diagram.
I suggest after reading the above info, you just draw it in a piece of paper
so that you understand /analyze better.
regds
maha anna


[This message has been edited by maha anna (edited March 15, 2000).]

Jim Yingst
sheriff
posted March 15, 2000 01:24 PM             
eram- I don't think these are ever really spelled out as states in the official documentation, but they can be inferred from how thread behavior is described in the JLS and in the API for the Thread and Object classes (wait(), notify(), and notifyAll() methods). Various books try to make this clearer - I recommend Roberts/Heller/Ernest's chapter on threads - they have some useful diagrams of state transitions.

eram
ranch hand
posted March 15, 2000 01:48 PM             
Thanks maha & jim.
That was useful.

|