wait(),notify()和notifyAll()都是java.lang.Object的方法:
wait(): Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object.
notify(): Wakes up a single thread that is waiting on this object's monitor.
notifyAll(): Wakes up all threads that are waiting on this object's monitor.
这三个方法,都是Java语言提供的实现线程间阻塞(Blocking)和控制进程内调度(inter-process communication)的底层机制。
1.使用条件 --获得锁
在调用wait(), notify()或notifyAll()的时候,必须先获得锁,且状态变量须由该锁保护
如:synchronized (xxxx) {
xxxx.wait();
}
不加锁使用这些方法会出现IllegalMonitorStateException异常.
原因: 在对某个对象上调用wait().notify 等方法进行线程等待或者唤醒(让其他竞争执行该代码的线程上锁)时,没有对该对象执行同步操作。
2.待完----