码迷,mamicode.com
首页 > 其他好文 > 详细

notify是否随机的?

时间:2019-06-30 19:18:01      阅读:273      评论:0      收藏:0      [点我收藏+]

标签:执行   The   mic   call   calling   str   存在   object   src   

当存在多个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的线程。

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

 

notify是否随机的?

标签:执行   The   mic   call   calling   str   存在   object   src   

原文地址:https://www.cnblogs.com/imaye/p/11110553.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!