Difference between WAIT and BLOCKED

What is the difference between WAIT state and BLOCKED state?

Thread.State Documentation :

Blocked
In this state, there is a thread that is blocked waiting for the monitor to lock.

Expectation
A thread that is waiting endlessly for another thread to perform a specific action is in this state

doesn't explain me the difference.

+45
java multithreading wait block
Mar 28 '13 at 11:18
source share
6 answers

A thread goes into a wait state when it calls wait() object. This is called a Pending state. Once the thread reaches the idle state, it will need to wait until some other notify() or notifyAll() object notifyAll() .

Once this thread is notified, it will not start. It is possible that other threads are also notified (using notifyAll() ), or the first thread has not completed its work, so it still blocks until it gets its chance. This is called Blocked .

After other threads remain and the likelihood of this thread, it enters the Runnable state after it has the right to recruit work based on the JVM streaming mechanism and enters the run state.

+33
Mar 28 '13 at 11:26
source share

The difference is relatively simple.

In the BLOCKED state BLOCKED thread is about to enter the synchronized block, but there is another thread that is currently running inside the synchronized block on the same object. The first thread must wait for the second thread to exit its block.

In the WAITING state, a thread is waiting for a signal from another thread. This usually happens when calling Object.wait() or Thread.join() . Then the thread will remain in this state until another thread calls Object.notify() or dies.

+53
Mar 28 '13 at 11:24
source share

A simplified perspective for interpreting thread dumps:
WAIT. I'm waiting to be given some kind of work, so now I'm idle. BLOCKED. I'm busy trying to get the job done, but there is another thread in my way, so I'm idle now. RUNNABLE ... (Native Method) - I called to run some kind of native code (which is not finished yet), since the JVM has the value RUNNABLE, and it can not give any additional information. A common example might be C's own socket listening method, which actually waits until any traffic arrives, so I am idle now. In this situation, this can be considered as a special kind of WAIT, since we actually do not work (without a processor), but you will have to use a dump of the operating system thread, rather than a Java thread dump, to see it.

+2
Feb 28 '16 at 10:17
source share

Blocked Your thread is in the current state of the thread's life cycle and is trying to obtain an object lock. Wait - your thread is waiting for the thread's life cycle state and waits until the notification signal enters the current thread state.

+1
Feb 03 '16 at 10:45
source share

An important difference between locked and pending states is the effect on the scheduler. A thread in a locked state is part of a waitset struggling to block; this thread is still considered to be something that the scheduler needs to serve, possibly influencing its decisions about how much time is required to execute the current threads.

Once the thread is on hold, the stress it imposes on the system is minimized, and the scheduler does not need to worry about it. It is inactive until it receives a notification. Except for the fact that it maintains a busy OS thread, it is completely out of the game.

That's why using notifyAll is less than ideal, it causes a bunch of threads that were previously inactive without loading the system to wake up, where most of them are blocked until they can get the lock, find the condition they are waiting for is incorrect, and go back to waiting. It would be preferable to notify only those threads that have a chance to make progress.

+1
May 7 '17 at 12:21
source share

see this example:

Demonstration of stream states.

 /*NEW- thread object created, but not started. RUNNABLE- thread is executing. BLOCKED- waiting for monitor after calling wait() method. WAITING- when wait() if called & waiting for notify() to be called. Also when join() is called. TIMED_WAITING- when below methods are called: Thread.sleep Object.wait with timeout Thread.join with timeout TERMINATED- thread returned from run() method.*/ public class ThreadBlockingState{ public static void main(String[] args) throws InterruptedException { Object obj= new Object(); Object obj2 = new Object(); Thread3 t3 = new Thread3(obj,obj2); Thread.sleep(1000); System.out.println("nm:"+t3.getName()+",state:"+t3.getState().toString()+ ",when Wait() is called & waiting for notify() to be called."); Thread4 t4 = new Thread4(obj,obj2); Thread.sleep(3000); System.out.println("nm:"+t3.getName()+",state:"+t3.getState().toString()+",After calling Wait() & waiting for monitor of obj2."); System.out.println("nm:"+t4.getName()+",state:"+t4.getState().toString()+",when sleep() is called."); } } class Thread3 extends Thread{ Object obj,obj2; int cnt; Thread3(Object obj,Object obj2){ this.obj = obj; this.obj2 = obj2; this.start(); } @Override public void run() { super.run(); synchronized (obj) { try { System.out.println("nm:"+this.getName()+",state:"+this.getState().toString()+",Before Wait()."); obj.wait(); System.out.println("nm:"+this.getName()+",state:"+this.getState().toString()+",After Wait()."); synchronized (obj2) { cnt++; } } catch (InterruptedException e) { e.printStackTrace(); } } } } class Thread4 extends Thread{ Object obj,obj2; Thread4(Object obj,Object obj2){ this.obj = obj; this.obj2 = obj2; this.start(); } @Override public void run() { super.run(); synchronized (obj) { System.out.println("nm:"+this.getName()+",state:"+this.getState().toString()+",Before notify()."); obj.notify(); System.out.println("nm:"+this.getName()+",state:"+this.getState().toString()+",After notify()."); } synchronized (obj2) { try { Thread.sleep(15000); } catch (InterruptedException e) { e.printStackTrace(); } } } } 
0
May 7 '17 at 11:02
source share



All Articles