Is Java Thread wait () => blocked?

According to the Java thread status information , calling wait () will cause the thread to be in the BLOCKED state. However, this piece of code will be called (after it is called) in the stream in the WAITING state.

class bThread extends Thread { public synchronized void run() { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } 

Is something wrong with me? Can someone explain this behavior to me? Any help would be appreciated!

+8
java concurrency
Mar 28 '10 at 19:12
source share
6 answers

Stream WAITING until it is notified. Then it becomes BLOCKED, trying to re-enter the synchronized area until all other threads remain.

Relevant parts from the link you posted (about WAITING):

For example, a thread calling Object.wait () on an object waits for another thread to call Object.notify () or Object.notifyAll () on that object.

and (about BLOCKED):

A thread in a blocked state expects a monitor lock to [...] re-enter the synchronized block / method after calling Object.wait.

The last part occurs when the thread tries to return from wait (), but not until then.

+15
Mar 28 '10 at 19:16
source share
— -

The monitor runs one thread at a time. Assuming you have T1-T10, 9 BLOCKED , and one is RUNNABLE . From time to time, the monitor selects a new thread to start. When this happens, the selected / current thread, say T1, goes from RUNNABLE to BLOCKED . Then another thread, say T2, goes from BLOCKED to RUNNABLE , becoming the current thread.

When one of the threads needs some information available to the other thread, you use wait() . In this case, the stream will be marked as WAITING until notify() ed. Thus, the thread that is waiting will not be executed by the monitor until then. For example, wait until the boxes are unloaded. The drawer loading box will notify me when this happens.

In other words, both BLOCKED and WAITING are inactive thread status, but WAITING thread cannot be RUNNABLE without first accessing BLOCKED . WAITING threads do not want to become active, while BLOCKED threads do want, but they cannot, because it is not their turn.

I think.

+9
Mar 28 '10 at 19:40
source share

Where have you seen him say such things?

On the same page that you linked, thread.state , it clearly states that

WAITING will be after Object.wait ()

BLOCKED will be before entering synchronized

+3
Mar 28 '10 at 19:20
source share

Waiting is when he does nothing at all. Blocked when it tries to start again, but not yet resolved.

+1
Mar 28 '10 at 19:15
source share

There is some confusing terminology. When a thread causes an object to wait, it enters the WAIT state. When threads wait for a lock lock, they belong to the wait set for this lock, but they are in the BLOCKED state.

It is embarrassing, but somehow it makes sense!

+1
Oct 29
source share

As a reminder, you should always call wait () inside the while loop, waiting for conditions to enter the synchronous area / critical section. This is because Java has “false awakenings” (essentially, a thread can wake up at any time for no reason).

0
Mar 28 '10 at 20:10
source share



All Articles