这是Object类中,wait方法的注释中的一段话,
这句话的意思是:wait()会释放对象的锁,所以调用wait的条件是一定要拥有对象的锁,所以wait方法应该放在synchronize里面,如下
package com.lin.thread;
public class ThreadWait {
private Object lock;
public ThreadWait(Object lock) {
this.lock = lock;
}
public void testWait(){
synchronized (lock) {
System.out.println("start wait...");
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("end wait ...");
}
}
}