Java:notify() 与 notifyAll() 重来一遍 [英] Java: notify() vs. notifyAll() all over again

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

问题描述

如果 Google 搜索notify()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.

notify()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() 返回的线程会离开条件为假状态,则其他被唤醒的线程将返回等待.

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:notify() 与 notifyAll() 重来一遍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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