wait和notify使用例子_ide

wait和notify使用例子_ide_02

public class Test2 {
public static void main(String[] args) {
String lock = "lock";
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock){
System.out.println("线程1开始等待" + System.currentTimeMillis());
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("线程1结束等待" + System.currentTimeMillis());
}
}
}){
};
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock){
System.out.println("线程2开始等待唤醒" + System.currentTimeMillis());
lock.notify();
System.out.println("线程2开始唤醒" + System.currentTimeMillis());
}
}
}){
};
thread1.start();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
thread2.start();
}
}