Thread Status Description
The thread state is defined in the Thread.state enumeration, and the following is taken from the Thread.java source
NEW:
Thread has not started
A thread that has not yet started are in this state.
RUNNABLE:
Thread can run, but may not be assigned to the processor
A thread executing in the Java virtual machine was in this state.
BLOCKED:
Thread waits for Monitor lock
A thread that's blocked waiting for a monitor lock are in this state.
Waiting:
Thread waits indefinitely for another thread to perform a specific action
A thread that's waiting indefinitely for another the thread to perform a particular action was in the this state.
Timed_waiting:
The thread waits for the other thread to perform a specific action, but waits for a specified time
A thread that's waiting for another the thread to perform an action for up to a specified waiting time was in this state.
TERMINATED:
Thread has exited
A thread that has exited is in the this state.
How to enter blocked, waiting, and timed_waiting states
BLOCKED:
A thread in the blocked state was waiting for a monitor lock to enter a synchronized block/method or reenter a synchronized Block/method after calling Object.wait.
A thread in blocked is waiting for the monitor lock to enter the synchronized block or synchronized method, or to enter the synchronized block or synchronized method again after calling Object.wait.
Waitting:
A thread is in the waiting state due to calling one of the following methods:
Object.wait with no timeout
Thread.Join with no timeout
Locksupport.park
A thread that calls the following method enters the waitting state
Object.wait with no timeouts
Thread.Join with no timeouts
Locksupport.park
Timed_waiting:
A thread is in the timed waiting state due to calling one of the following methods with a specified positive waiting time.
Thread.Sleep
Object.wait with timeout
Thread.Join with timeout
Locksupport.parknanos
Locksupport.parkuntil
A thread that calls the following method enters the timed_waiting state
Thread.Sleep
Object.wait with time-outs
Thread.Join with time-outs
Locksupport.parknanos
Locksupport.parkuntil
About the difference between blocked and waiting
The blocked state is waiting for the monitor lock and whether the lock is available is notified by the JVM. In the waitting state, it is possible to continue being notified by other threads.
Refer to two articles:
Thread.state in Java? BLOCKED vs Waiting
http://blog.csdn.net/teaey/article/details/20059129
What's thread.state in Java? What's it used for?
Thread.state-this is a static nested class (Read more about nested classes in the article-nested classes & Inner C Lasses in Java >>) of the Thread class. This was one of the additions of Java 5 and this class actually inherits the abstract class Enum which is the common base C Lass of all Java language enumeration types i.e., thread.state are actually is actually a enumeration type.
Thread.state enumeration contains the possible states of a Java thread in the underlying JVM. These states is different from the Operating System thread states. The possible values of the Thread.state are:-
New -This state represents a new thread which are not yet started.
RUNNABLE -This state represents a thread which was executing in the underlying JVM. Here executing in JVM doesn ' t mean that the thread was always executing in the OS as well-it could wait for a resource from The Operating system like the processor and being in.
BLOCKED -This state represents a thread which have been BLOCKED and is waiting for a moniotor to enter/re-enter a Synchronized Block/method. A thread gets into the calling object.wait method.
waiting -This state represnts a thread in the waiting state and this wait was over only if some other thread PE Rforms some appropriate action. A thread can get into this state either by calling-object.wait (without timeout), Thread.Join (without timeout), or Lock Support.park methods.
timed_waiting -This state represents a thread which was required to wait on Max for a specified time limit. A thread can get into the calling either of these methods:Thread.sleep, object.wait (with timeout specified), Th Read.join (with timeout specified), Locksupport.parknanos, Locksupport.parkuntil
TERMINATED -This state reprents a thread which have completed its execution either by returning from the run () Me Thod after completing the execution OR by throwing a exception which propagated from the run () method and hence caused th e Termination of the thread.
Difference between BLOCKED state and waiting/timed_waiting states?
When a thread calls object.wait method, it releases all the acquired monitors and was put into waiting (or timed_waiting if We call the timeout versions of the Waitmethod) state. Now, the thread is notified either by Notify (), or by Notifyall (), the same object then the Thread ends and the thread starts attempting to regain all the monitors which it had acquired at the time of the wait call. At one time there is several threads trying to regain (or maybe gain for the first time) their monitors. If more than one threads attempt to acquire the monitor of a particular object and only one thread (selected by the JVM s Cheduler) is granted, the monitor and all other threads are put into BLOCKED state. Got the difference?
The difference between blocked and waiting/timed_waiting
When a thread calls object.wait, it releases the monitor lock it has acquired and then enters the waiting or timed_waiting state. If the thread now receives a notify or notifyall notification from another thread on this monitor lock, the thread will end the wait state and try again to get the monitor lock that was acquired when wait was called. At this point there may be other threads re-or get their respective monitor locks for the first time, if more than one thread gets the same monitor lock, then only one thread can get to, and the other threads will enter the blocked state.
Difference between waiting and timed_waiting states?
The difference is quite obvious between the. A thread in a timed_waiting state would wait at max for the specified timeout period whereas a thread in the waiting state Keeps waiting for an indefinite period of time. For example, if a thread have calledobject.wait method to put itself into waiting state then it ' ll keep waiting until the T Hread is interrupted either by Notify () method (OR by Notifyall () method), the same object by another thread. Similarly, if a thread has put itself to waitingstate by calling Thread.Join method then it ' ll keep waiting until the SP Ecified thread terminates.
We can easily figure out this a thread in a waiting state would always be dependent on an action performed by some other th Read whereas a thread in timed_waiting isn't completely dependent on an action performed by some other thread as in this Case the wait ends automatically after the completion of the timeout period.
Blocked vs Waiting Thread
Http://www.coderanch.com/t/425685/threads/java/Blocked-Waiting-Thread
Alain Dickson Q:
Hi ranches, need your help to clear a point,
Is there a difference in Thread's state when it encouters following the situations. I have question on Blocked state.
Here's how I understand the states.
1. (Waiting). The thread enters synchronized block and calls Wait () on the object whos lock it already has and waits till some other th Read notify it.
2 (BLOCKED). The thread makes a calls to Synchronized method whos lock are already acquired by some other Thread and gets blocked.
Question:once a thread is blocked, does it wait quietly for someone Or some event to tell him that the lock is available (Just like wait/notify) OR it keeps on trying it until it gets lock.
Many thanks,
Alain
The questioner's understanding of the two States is
1. Waiting: The thread is already in the Synchronized method and calls the wait () method that already holds the lock, waiting for the other thread to notify it
2. BLOCKED: Thread invokes synchronized method that has been acquired by another thread
The question that the questioner wants to ask is
If the thread is in the blocked state, is the thread trying to get the lock (like a busy, etc) all the time, or wait for a thread or event to notify it that the lock is available (similar to wait/notify)?
Mike Simmons's Answer:
It would keep trying until it acquires the lock.
Well, we could also say this it waits quietly for the JVM to tell it's the lock is available. It's similar to wait/notify, but you, the programmer, don ' t has to do anything. The JVM would do it for you.
The gist of the answer is:
The thread waits for the JVM to notify it that the lock is available, similar to wait/notify, but the caller does not care
In response to Mike's answer "We could also say that it waits quietly for the JVM ' to ' it that's the lock is available. It's similar to Wait/notify ", Alain Dickson asked:
Well, can we say for sure that the Thread which is blocked, waits until the lock becomes available
and that:
1. It does not wake up in-between to check if the lock is available
2. When the lock becomes available, which notification JVM uses notify () OR Notifyall ()
Thanks,
Alain
The effect is:
is not that we can say for sure that the blocked thread will wait until the lock is available and
1. Thread does not wake up in the middle to check if the lock is available
2. When the lock is available, the JVM uses notify or notifyall notification
Mike Simmons's Answer:
For the first question: 1. It does not wake up in-between to check if the lock is available
For sure? Probably Not-i think the exact mechanism is implementation-dependent.
Note that I should not having said "wait" above, since in thread discussions the has a specific meaning which does not appl y here. We can say that the thread, in general, does nothing until the lock is acquired. I Suppose it ' s possible that in some implementations, threads might occasionally "wake up" and check their status. But whatever they does, you can assume it ' s something reasonably efficient. The threads does not, for example, spend all their time in loops, checking and rechecking their status. That would is a waste of resources, similar to a "busy wait".
When you asked if a thread ' keeps on trying until it gets the lock ', my concern was ' so you might think ' the thread is ' was Ting resources by repeatedly waking up and asking if the lock is available yet. In general, no, it doesn ' t. It's possible that some implementations could do this occasionally. But I wouldn ' t worry about it.
In response to the first question, Mike thought it was impossible to be so sure that the implementation was relevant. Mike points out that asking questions like this is a waste of resources considering repeated wake-up checks, and that in some implementations, blocked threads are likely to wake up intermittently to check if a lock is available, but certainly not spend all the time checking it, without worrying too much.
For a second question: 2. When the lock becomes available, which notification JVM uses notify () OR Notifyall ()
Neither of these. I said that the JVM does something like wait/notify-but it doesn ' t actually use wait (), notify (), or notifyall ().
However, I would guess that's what the it does is probably more like notify () than Notifyall (). There ' s no good reason to bother all the threads that is trying to acquire a lock. Better to pick just one, give it the lock, and tell it to run.
In response to the second question, Mike believes that a mechanism similar to notify or notifyall is used, but not notify or notifyall. And Mike guesses that it's more about using similar notify, after all, it's not necessary to notify all the threads to get the lock.
In the middle there was an interruption called Chris Hurst:
Adaptive locking is supposed to come in with Mustang and allow Java to make a educated guess as to if spinning or suspen Sion of the thread is more efficient in a blocked scenario.
The main idea is that adaptive locks should be introduced, allowing Java to reasonably guess whether it is spin or hang.
The questioner then raises the question he really wants to ask:
Alain Dickson:
Thanks Mike,
Though from the discussion it makes sense both sychronized method calls and Wait/notify is similar systems in terms of BL Ocking thread and make it wait for I would like to explicitly ask That:i has a situation where the design suggests to u Se wait/notify but I am thinking of redesigning it into synchronized method calls where (behaviour of wait/notify) was done By JVM calling wait and notify Explicity, which are prone to errors as compared to calls to synchronized me Thods. If The design allows, would synchronized methods give similar performance as wiat/notify where I know no thread would wake u P until I call Notify/notifyall?
Thanks,
Alain
What the questioner really wants to ask is that since synchronized is a similar notify mechanism, there is now a design that originally suggested using wait and notify, but because this method is prone to error, he wants to use synchronize redesign, performance and wait/ Like notify.
Mike Simmons:
That's a interesting idea. Yes, I would expect that both systems would probably give similar performance. If one is faster, I don ' t know which it would be-could be either. Probably the performance would be close enough either the it doesn ' t matter, and your decision would be based on other Considerations.
Mike's answer is:
It is not possible to confirm which performance is better, and the performance of the two may be very close.
Alain Dickson Thanks to the respondents Mike:
Thanks for your valueable openion Mike.
Regards,
Alain
And there's another one who answers Steve Luke:
A wait/notify system usually indicates that one thread can do some work concurrently with another, but have to specifically Wait for the other thread to reach a certain point before it can proceed (say a consumer which can work on data, but must Wait for the producer to make the data available before getting the next piece). Since you don ' t has control over thread scheduling the only thing you can be call wait () to force the consumer to wait Until the producer notify () s it of completion.
Without wait/notify (or the condition.await ()/signal () from the concurrent API) you can ' t really get synchrony points poin TS between different threads because you can ' t be sure which threads would get processor time and get the lock on the synch Ronized object. For example the consumer could get a point where it had to block on a synchronized lock held by the producer. The producer releases the lock, but the thread maintains processor time, regains the lock, and keeps working. The poor consumer is still left in the blocking state, never has the chance to gain the synchronizing lock.
So if is looking to synchronize II threads in the fashion Wait ()/notify () allows, I think it would be rather difficu Lt to execute using just synchronized methods because it would is hard-make sure any blocking thread ever gets some run time when it was needed. You might want-to-look at some of the classes in the Java.util.concurrent package to make things easier, like a blockingqu Eue to pass signals through, a Semaphore-provide permissions for the consumer-run, lock/conditions for a different s ynchronized lock/conditional wait and notification scheme, or the cyclicbarrier to allow multiple threads to come together To a single synchronized point. Many other options as well.
Steve's main idea is:
Because we can not control the scheduling, if there is no wait/notify system, can not really get to the synchronization point, just using the Synchronized method is also difficult to synchronize in some cases, using some other classes may be more simple, such as Blockingqueue, Semaphore, Cyclicbarrier and so on.
Alain Dickson thanks to Steve:
Thanks Steve,
I have achieved my solution using the Cocurrent package. It is very flexible and interesing.
Regards,
Alain
This article is from the "self-reliance, tenet" blog, please be sure to keep this source http://wangzhichao.blog.51cto.com/2643325/1742126
About Java Thread State