6

What is difference between the BLOCKED and the WAITING states of a thread.

As per JAVA code comments

    /**
     * Thread state for a thread blocked waiting for a monitor lock.
     * A thread in the blocked state is waiting for a monitor lock
     * to enter a synchronized block/method or
     * reenter a synchronized block/method after calling
     * {@link Object#wait() Object.wait}.
     */
    BLOCKED,

    /**
     * Thread state for a waiting thread.
     * A thread is in the waiting state due to calling one of the
     * following methods:
     * <ul>
     *   <li>{@link Object#wait() Object.wait} with no timeout</li>
     *   <li>{@link #join() Thread.join} with no timeout</li>
     *   <li>{@link LockSupport#park() LockSupport.park}</li>
     * </ul>
     *
     * <p>A thread in the waiting state is waiting for another thread to
     * perform a particular action.
     *
     * For example, a thread that has called <tt>Object.wait()</tt>
     * on an object is waiting for another thread to call
     * <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on
     * that object. A thread that has called <tt>Thread.join()</tt>
     * is waiting for a specified thread to terminate.
     */
    WAITING,

Doesn't it mean that both of the states are similar in terms of waiting for lock ?

10

I think the comments you posted give the answer. They are similar in that a thread is waiting for another thread. But they are not the same.

The BLOCKED state means waiting for an object's monitor. This often happens when a synchronized method or statement is used. The thread is waiting for access to synchronized code, or to do any other action that requires owning the object's monitor. This kind of blocking would often happen when there is no attempt and coordinate between threads, but there are two or more threads calling an object's synchronized methods or statements.

WAITING means waiting for a specific action, e.g. a Notify, or using join() to wait for a thread to terminate. This kind of waiting seems to indicate some attempt at coordination between threads, where one thread notifies another or waits for another to complete.

One potentially confusing factor is that calling Object.wait() requires owning the object's monitor, in order to begin waiting. But Object.wait() then releases the monitor until notified. So a thread might go through BLOCKED state to WAITING state as it obtains the monitor, then executes Object.wait() and begins waiting. See Doc: Java 7 Object.wait().

Java 7 Docs appear to confirm this interpretation...

A thread state. A thread can be in one of the following states:

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.

A thread can be in only one state at a given point in time. These states are virtual machine states which do not reflect any operating system thread states.

Java 7 Docs Thread.State

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.