Java中wait与notify的正确使用
来源:互联网 发布:python if else 简写 编辑:程序博客网 时间:2024/05/21 09:39
今天编程时遇到一个wait方法调用时抛出IIIegalMonitorStateException异常,所以再把wait和notify的用法整理一遍。
首先,多线程同步问题既有多线程对资源的竞争问题,也有多线程之间的协同问题。在这里我们仅讨论多线程的协同问题。
1、wait()与notifyAll()
wait()在Java doc中的描述为:
Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object. In other words, this method behaves exactly as if it simply performs the call wait(0).The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAllmethod. The thread then waits until it can re-obtain ownership of the monitor and resumes execution.As in the one argument version, interrupts and spurious wakeups are possible, and this method should always be used in a loop: synchronized (obj) { while (<condition does not hold>) obj.wait(); ... // Perform action appropriate to condition }This method should only be called by a thread that is the owner of this object's monitor. See the notify method for a description of the ways in which a thread can become the owner of a monitor.
上述描述说明,wait()方法只能在拥有指定对象的控制权时才能被调用。具体怎么样获得对象的控制权,要参看notify方法的描述。
notify()在java doc中的描述为:
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.The awakened thread will not be able to proceed until the current thread relinquishes the lock on this object. The awakened thread will compete in the usual manner with any other threads that might be actively competing to synchronize on this object; for example, the awakened thread enjoys no reliable privilege or disadvantage in being the next thread to lock this object.This method should only be called by a thread that is the owner of this object's monitor. A thread becomes the owner of the object's monitor in one of three ways:By executing a synchronized instance method of that object.By executing the body of a synchronized statement that synchronizes on the object.For objects of type Class, by executing a synchronized static method of that class.Only one thread at a time can own an object's monitor.
上述描述说明,notify()方法会随机唤醒一个等待对象的线程,被唤醒的线程只有在调用notify()的线程释放对象锁后才可能执行,另外被唤醒的线程与其他等待该对象锁的线程都同等的概率称为获得对象锁的下一个线程。
notify()方法也只能在拥有对象控制权的线程中被调用,一个线程称为对象控制权的所有者的方法有三种:
* 执行该对象的同步的实例方法
* 执行该对象的同步块
* 对于该对象的类,执行该类的静态同步方法
综上可知,wait()和notify()方法、notifyAll()方法在调用时必须放在同步代码中。如果在非同步控制方法中调用这些方法,程序能编译通过,但是运行时将得到IIIegalMonitorStateException异常。
wait()的用法如下:
synchronized (obj) { while (<condition does not hold>) obj.wait(); ... // Perform action appropriate to condition }
notify的用法如下:
synchronized(obj) { obj.notify();}
0 0
- Java中wait与notify的正确使用
- 如何在 Java 中正确使用 wait, notify 和 notifyAll
- 如何在 Java 中正确使用 wait, notify 和 notifyAll
- 如何在 Java 中正确使用 wait, notify 和 notifyAll
- 如何在 Java 中正确使用 wait, notify 和 notifyAl
- 如何在 Java 中正确使用 wait, notify 和 notifyAll
- Java 内部类,多线程中Synchronized与wait,notify的使用
- java中wait 信号量和notify信号量的使用
- Java多线程中wait, notify and notifyAll的使用
- Java多线程中wait, notify and notifyAll的使用
- java中wait()、notify()以及notifyAll()的使用
- Java多线程中wait, notify and notifyAll的使用
- Java 中正确使用 wait, notify 和 notifyAll – 以生产者消费者模型为例
- 在Java中正确使用wait, notify和notifyAll – 以生产者消费者模型为例
- wait()-notify()的正确写法
- java同步中wait 与 notify
- java 多线程中wait/notify使用
- java,wait与notify
- ldr和ldr伪指令
- OpenGL ES 3.0 Programming Guide(second edition)针对中文版翻译的补充——第一章
- 中间缓存变量机制
- Activity的生命周期和启动模式
- linux停止正在执行脚本
- Java中wait与notify的正确使用
- 使用异步 I/O 大大提高应用程序的性能《转载》
- Linux上C语言的自我学习一(文件编程-操作篇)
- DOM解析XML
- Python3教程之《开发常用工具》
- QT界面与视频同时显示在屏幕上
- 使用Eclipse开发和调试java程序
- Continuous integration for Xcode Project
- Tomcat 热部署