void java.lang.
Object.notifyAll()
Causes all threads which are waiting on this object's monitor (by means of calling one of the wait()
methods) to be woken up. The threads will not run immediately. The thread that called notify()
has to release the object's monitor first. Also, the threads still have to compete against other threads that try to synchronize on the same object.
This method can only be invoked by a thread which owns this object's monitor. A thread becomes owner of an object's monitor
- by executing a synchronized method of that object;
- by executing the body of a
synchronized
statement that synchronizes on the object; - by executing a synchronized static method if the object is of type
Class
.
-
Throws:
- IllegalMonitorStateException - if the thread calling this method is not the owner of this object's monitor. See Also:
- notify
- wait()
- wait(long)
- wait(long, int)
- java.lang.Thread
- 作用:唤醒所有在object对象上调用wait()方法的线程,线程不一定会立即执行。调用了notify()方法的线程会首先释放object锁,而且线程仍然需要和其他线程竞争来同步同一个对象。此方法只能唤醒拥有对象锁的线程。一个对象要拥有一个对象锁,可以通过下面三个方法:
- 1. 执行此对象的一个同步方法
- 2. 执行同步此对象的方法块
- 3. 如果对象是Class类型,执行一个同步静态方法
- 抛出异常: IllegalMonitorStateException 如果调用notifyAll()的线程不是锁的拥有者