Java Threads may have 6 states: new, runnable, terminated, blocked, waiting, timed_waiting.
When a thread is created it is in new state.
When start method of thread is called it enters runnable state. Runnable
state has two inner states: ready and running. If thread is
eligible for execution it is said to be ready, if it is executing it is in running
state. Remember calling start method on a already started thread will raise
IllegalThreadStateException.
When thread finishes its execution it enters into terminated state.
When a thread is trying to access a resource, a synchronized statement for
example, and it is not available, lock of the object is already acquired for
example, it is blocked and said to be in blocked state. When lock is released
an thread has chance to acquire lock it goes back to runnable state.
When a thread calls join or wait method it enters into waiting state. When
joined thread finishes or for wait method notify/notifyAll method is called
thread goes back to runnable state. Remember wait method releases locks, while
join does not.
When a thread calls overriden versions of join or wait methods ,which has a
time out argument, or sleep method, it enters into timed_waiting state. When
time out is completed thread goes back to runnable state again. Remember wait
method releases locks, while join and sleep does not.
great
ReplyDelete