Loading

notify是否随机的?

当存在多个wait的线程时,一次notify总是启动的第一个wait的线程,但notify注解不是说随机的么/
自己多次执行都是第一个wait的线程先执行???

先来看1.8java的关于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 {@code wait} methods.
  ....
此处说是arbitrary(任意) but 还有个and 然后
discretion of the implementation的谷歌翻译出来的意思"实施的自由裁量权"

ok fine you win/

网上有些人观点是native方法可以不用去管,有个观点帮助到我“取决于厂商的具体实现”

但谷歌翻译。。。于是我去有道🤣给出的例句中的意思“实现做出决定时发生”,

那看来就是notify方法具体要看实现方式。继续找notify的源码,得出的结果是由hostspot虚拟机实现,

好吧/我是小白/虚拟机的不了解/需要了解的可取查monitor的实现方式。

在HotSpot虚拟机中,monitor采用ObjectMonitor实现。

大体意思是wait后生成ObjectWaiter并保存刀wait set中,执行notify时

时从waitset中取出第一个ObjectWaiter节点,具体可查源码。

ok得出的结果notify是按顺序来执行的/但其他虚拟机呢?也就是说notify不一定是按顺序

唤醒wait的线程。

暂得出的结果如上/有问题可讨论哈/

 


posted @ 2019-06-30 17:41  ichar  阅读(1393)  评论(1编辑  收藏  举报