Java:再次通知()和notifyAll() [英] Java: notify() vs. notifyAll() all over again

查看:115
本文介绍了Java:再次通知()和notifyAll()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果一个谷歌用于之间的差异通知() notifyAll(),那么很多解释将会弹出(将javadoc段落分开)。这一切都归结为等待线程被唤醒的数量:一个在 notify(),所有在 notifyAll()

If one Googles for "difference between notify() and notifyAll()" then a lot of explanations will pop up (leaving apart the javadoc paragraphs). It all boils down to the number of waiting threads being waken up: one in notify() and all in notifyAll().

但是(如果我确实理解了这些方法之间的区别),总是只选择一个线程进行进一步的监视器获取;在第一种情况下,由VM选择的一种情况,在第二种情况下是由系统线程调度程序选择的一种情况。程序员不知道两者的确切选择程序(在一般情况下)。

However (if I do understand the difference between these methods right), only one thread is always selected for further monitor acquisition; in the first case the one selected by the VM, in the second case the one selected by the system thread scheduler. The exact selection procedures for both of them (in the general case) are not known to the programmer.

有用之间的差异是什么href =http://download.oracle.com/javase/6/docs/api/java/lang/Object.html#notify%28%29 =noreferrer> notify()和< a href =http://download.oracle.com/javase/6/docs/api/java/lang/Object.html#notifyAll%28%29 =noreferrer> notifyAll()然后?我错过了什么吗?

What's the useful difference between notify() and notifyAll() then? Am I missing something?

推荐答案


但是(如果我理解这些方法之间的区别吧) ,只选择一个线程进行进一步的监视器获取。

However (if I do understand the difference between these methods right), only one thread is always selected for further monitor acquisition.

这是不正确的。 o.notifyAll()唤醒所有中阻止的线程o.wait()电话。线程只允许从 o.wait()逐个返回,但每个轮到他们。

That is not correct. o.notifyAll() wakes all of the threads that are blocked in o.wait() calls. The threads are only allowed to return from o.wait() one-by-one, but they each will get their turn.

简单地说,这取决于您的线程等待通知的原因。你想告诉其中一个等待线程发生了什么事,或者你想同时告诉所有这些线程吗?

Simply put, it depends on why your threads are waiting to be notified. Do you want to tell one of the waiting threads that something happened, or do you want to tell all of them at the same time?

在某些情况下,所有等待的线程等待完成后可以采取有用的行动。一个例子是等待某个任务完成的一组线程;一旦任务完成,所有等待的线程都可以继续他们的业务。在这种情况下,您可以使用 notifyAll()同时唤醒所有等待的线程。

In some cases, all waiting threads can take useful action once the wait finishes. An example would be a set of threads waiting for a certain task to finish; once the task has finished, all waiting threads can continue with their business. In such a case you would use notifyAll() to wake up all waiting threads at the same time.

另一种情况,例如互斥锁定时,只有一个等待的线程在收到通知后可以做一些有用的事情(在这种情况下获取锁)。在这种情况下,您宁愿使用 notify()。正确实施后,你可以在这种情况下使用 notifyAll(),但你会不必要地唤醒那些无论如何也无法做任何事情的线程。

Another case, for example mutually exclusive locking, only one of the waiting threads can do something useful after being notified (in this case acquire the lock). In such a case, you would rather use notify(). Properly implemented, you could use notifyAll() in this situation as well, but you would unnecessarily wake threads that can't do anything anyway.

在许多情况下,等待条件的代码将被写为循环:

In many cases, the code to await a condition will be written as a loop:

synchronized(o) {
    while (! IsConditionTrue()) {
        o.wait();
    }
    DoSomethingThatOnlyMakesSenseWhenConditionIsTrue_and_MaybeMakeConditionFalseAgain();
}

这样,如果 o.notifyAll() 调用唤醒多个等待线程,并且从 o.wait()返回的第一个使得条件处于false状态,然后被唤醒的其他线程将返回等待。

That way, if an o.notifyAll() call wakes more than one waiting thread, and the first one to return from the o.wait() makes leaves the condition in the false state, then the other threads that were awakened will go back to waiting.

这篇关于Java:再次通知()和notifyAll()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆