wait(), notify() and notifyAll()
来源:互联网 发布:大数据资格认证 编辑:程序博客网 时间:2024/06/05 20:26
在Java的Object类中有三个final的方法允许线程之间进行资源对象锁的通信,他们分别是: wait(), notify() and notifyAll()。
调用这些方法的当前线程必须拥有此对象监视器,否则将会报java.lang.IllegalMonitorStateException exception异常。
wait
Object的wait方法有三个重载方法,其中一个方法wait() 是无限期(一直)等待,直到其它线程调用notify或notifyAll方法唤醒当前的线程;另外两个方法wait(long timeout) 和wait(long timeout, int nanos)允许传入 当前线程在被唤醒之前需要等待的时间,timeout为毫秒数,nanos为纳秒数。
notify
notify方法只唤醒一个等待(对象的)线程并使该线程开始执行。所以如果有多个线程等待一个对象,这个方法只会唤醒其中一个线程,选择哪个线程取决于操作系统对多线程管理的实现。
notifyAll
notifyAll 会唤醒所有等待(对象的)线程,尽管哪一个线程将会第一个处理取决于操作系统的实现。
这些方法可以使用于“生产者-消费者”问题,消费者是在队列中等待对象的线程,生产者是在队列中释放对象并通知其他线程的线程。
让我们来看一个多线程作用于同一个对象的例子,我们使用wait, notify and notifyAll方法。
阅读全文
0 0
- wait(), notify() and notifyAll()
- synchronized , wait() , notify() and notifyAll()
- wait(), notify() and notifyAll() in Java
- Java Thread wait, notify and notifyAll Example
- Java 线程wait, notify and notifyAll 示例
- Java线程中的wait, notify and notifyAll
- wait、notify、notifyAll
- notify();wait();notifyALL();
- wait() notify() notifyAll()
- wait、notify、notifyAll
- wait notify notifyAll
- wait(),notify(),notifyAll()
- wait(),notify(),notifyAll()
- wait,notify,notifyall
- sleep & wait | notify | notifyAll
- wait()与notify()/notifyAll()
- wait notify notifyall
- wait()、notify()、notifyAll()
- 工作所用的日常 Git 命令
- 页码统计/牛客网/Python/解题报告+源代码
- linux 安装svn
- LeetCode 122. Best Time to Buy and Sell Stock II
- nginx负载均衡
- wait(), notify() and notifyAll()
- Ubuntu下安装并配置VS Code编译C++
- hdu6184 (过题全靠抖
- DBCP2 连接池配置
- TLS/SSL 协议详解(11) Server Certificate
- 3.单件模式
- Android 6.0以上 解决recyclerview 在 scrollview 中不能全部显示,高度不正常的问题
- 实验楼Git实战教程-初始化 Git 仓库
- Android控件之ExpandableListView多级列表