How notify method works

此生再无相见时 提交于 2019-12-04 04:47:51

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/16403764/how-notify-method-works

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!