Java 线程等待()=> 被阻塞?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2534147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Thread wait() => blocked?
提问by Chris
According to Java thread state infocalling wait() will result a thread to go in BLOCKED state. However this piece of code will result (after being called) in a Thread in WAITING State.
根据Java 线程状态信息,调用 wait() 将导致线程进入 BLOCKED 状态。然而,这段代码将导致(在被调用后)处于 WAITING 状态的线程。
class bThread extends Thread {
public synchronized void run() {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Have I got something wrong? Can anybody explain this behaviour to me? Any help would be appreciated!
我有什么问题吗?有人可以向我解释这种行为吗?任何帮助,将不胜感激!
采纳答案by Josef Grahn
The thread is WAITING until it is notified. Then it becomes BLOCKED trying to reenter the synchronized region until all other threads have left.
线程在等待,直到收到通知。然后它变成 BLOCKED 试图重新进入同步区域,直到所有其他线程都离开。
Relevant parts from the link you posted (about WAITING):
您发布的链接中的相关部分(关于 WAITING):
For example, a thread that has called Object.wait() on an object is waiting for another thread to call Object.notify() or Object.notifyAll() on that object.
例如,在对象上调用 Object.wait() 的线程正在等待另一个线程在该对象上调用 Object.notify() 或 Object.notifyAll()。
and (about BLOCKED):
和(关于 BLOCKED):
A thread in the blocked state is waiting for a monitor lock to [...] reenter a synchronized block/method after calling Object.wait.
处于阻塞状态的线程正在等待监视器锁 [...] 在调用 Object.wait 后重新进入同步块/方法。
The last part occurs when the thread tries to return from wait(), but not until then.
最后一部分发生在线程试图从 wait() 返回时,但直到那时才返回。
回答by AaronM
Waiting is when it's not doing anything at all. Blocked is when it's trying to start running again but hasn't been allowed to yet.
等待是它什么都不做的时候。阻止是当它尝试再次开始运行但尚未被允许时。
回答by Yoni Roit
Where did you see it say stuff like that?
你在哪里看到它说这样的话?
In the same page you linked, thread.state, it clearly states that
在您链接的同一页面中,thread.state,它清楚地指出
WAITING will be after Object.wait()
WAITING 将在 Object.wait() 之后
BLOCKED will be before entering synchronized
BLOCKED 将在进入同步之前
回答by zxcvbnm
The monitor executes one thread at a time. Assuming you have T1-T10 threads, 9 are BLOCKED
and one is RUNNABLE
. Every once in a while, the monitor picks a new thread to run. When that happens, the chosen/current thread, say T1, goes from RUNNABLE
to BLOCKED
. Then another thread, say, T2, goes from BLOCKED
to RUNNABLE
, becoming the current thread.
监视器一次执行一个线程。假设您有 T1-T10 线程,则 9 个是BLOCKED
,一个是RUNNABLE
。每隔一段时间,监视器就会选择一个新线程来运行。发生这种情况时,所选/当前线程(例如 T1)从RUNNABLE
到BLOCKED
。然后另一个线程,比如 T2,从BLOCKED
到RUNNABLE
,成为当前线程。
When one of the threads needs some information to be made available by another thread, you use wait()
. In that case, the thread will be flagged as WAITING
until it is notify()
ed. So, a thread that is waiting will not be executed by the monitor until then. An example would be, wait until there are boxes to be unloaded. The guy loading boxes will notify me when that happens.
当其中一个线程需要其他线程提供某些信息时,您可以使用wait()
. 在这种情况下,线程将被标记为WAITING
直到它被notify()
编辑。因此,在此之前,监视器不会执行正在等待的线程。一个例子是,等到有箱子要卸载。装箱的人会在发生这种情况时通知我。
In other words, both BLOCKED
and WAITING
are status of inactive threads, but a WAITING
thread cannot be RUNNABLE
without going to BLOCKED
first. WAITING
threads "don't want" to become active, whereas BLOCKED
threads "want" to, but can't, because it isn't their turn.
换句话说,BLOCKED
和WAITING
都是非活动线程的状态,但一个WAITING
线程RUNNABLE
不能不先去BLOCKED
。WAITING
线程“不想”变得活跃,而BLOCKED
线程“想要”但不能,因为轮到他们了。
I think.
我认为。
回答by JaHei
Just as a reminder, you should always call wait() inside a while loop waiting on the condition for entering the synchronized region/critical section. This is because Java has "spurious wakeups" (essentially, a thread can wakeup at any moment for no reason).
提醒一下,您应该始终在等待进入同步区域/临界区的条件的 while 循环中调用 wait()。这是因为 Java 有“虚假唤醒”(本质上,线程可以在任何时候无缘无故地唤醒)。
回答by Victor Grazi
There is some confusing terminology going on here. When a thread calls wait on an object it goes into the WAIT state. When threads are waitingto grab a lock, they belong to the waitset for that lock, but they are in the BLOCKED state.
这里有一些令人困惑的术语。当一个线程对一个对象调用 wait 时,它进入 WAIT 状态。当线程正在等待获取锁时,它们属于该锁的等待集,但它们处于 BLOCKED 状态。
Confusing but somehow it makes sense!
令人困惑,但不知何故它是有道理的!