通知方法如何工作 [英] How notify method works

查看:123
本文介绍了通知方法如何工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据javadoc通知唤醒正在此对象监视器上等待的单个线程。如果任何线程正在等待此对象,则选择其中一个线程被唤醒。选择是任意的,由实施决定。一个线程通过调用其中一个等待方法在对象的监视器上等待。



我想知道notify如何实现这种行为。在我读过的许多网站上发送信号但信号在这里意味着什么?



notify是否直接向第一个等待线程发送信号,或者向线程调度程序发送信号?

解决方案

它不会将它发送到第一个线程,而是发送到任何等待线程(但只有一个线程)。



你应该阅读 javadoc更仔细。



信号也可能是一个坏名字,'通知'更好。该机制的实际实现对Java程序员是透明的,并且您不需要技术实现,除非您对Java内部感兴趣,然后我建议您检查OpenJDK源代码。 / p>

As per the javadoc notify Wakes up a single thread that is waiting on this object's monitor. If any threads are waiting on this object, one of them is chosen to be awakened. The choice is arbitrary and occurs at the discretion of the implementation. A thread waits on an object's monitor by calling one of the wait methods.

I want to know how notify achieve this behavior. On many sites I read it sends a signal but What does signal means here?

Does notify sends a signal directly to first waiting thread or it sends a signal to thread scheduler?

解决方案

It does not send it to the first thread, but to any waiting thread (but only one thread).

You should read the javadoc more carefully.

Signal is also possibly a bad name for it, 'notify' is better. The actual implementation of the mechanism is transparent to the Java programmer, and the technical implementation should not be necessary for you to know.... unless you have an interest in Java internals, and then I suggest you inspect the OpenJDK source code.

这篇关于通知方法如何工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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