public class Demo01 {
public static void main(String[] args) {
Object obj = new Object();
new Thread(){
@Override
public void run() {
System.out.println("等待");
synchronized (obj){
try {
obj.wait(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("唤醒");
}
}.start();
}
}
public class Demo01 {
public static void main(String[] args) {
Object obj = new Object();
new Thread() {
@Override
public void run() {
System.out.println("顾客1等待");
synchronized (obj) {
try {
obj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("顾客1唤醒");
}
}.start();
new Thread() {
@Override
public void run() {
System.out.println("顾客2等待");
synchronized (obj) {
try {
obj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("顾客2唤醒");
}
}.start();
new Thread() {
@Override
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (obj) {
System.out.println("5s以后");
System.out.println("包子好了");
obj.notifyAll();
}
}
}.start();
}
}