public classWaitAndNotify {public static voidmain(String[] args) {
MethodClass methodClass= newMethodClass();
Thread t1= new Thread(() ->{try{
methodClass.product();
}catch(InterruptedException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
},"t1");
Thread t2= new Thread(() ->{try{
methodClass.customer();
}catch(InterruptedException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
},"t2");
Thread t3= new Thread(() ->{try{
methodClass.customer();
}catch(InterruptedException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
},"t3");
t1.start();
t2.start();
t3.start();
}
}classMethodClass {//定义生产最大量
private final int MAX_COUNT = 20;int productCount = 0;public synchronized void product() throwsInterruptedException {while (true) {
System.out.println(Thread.currentThread().getName()+ ":::run:::" +productCount);
Thread.sleep(10);if (productCount >=MAX_COUNT) {
System.out.println("货舱已满,,.不必再生产");
wait();
}else{
productCount++;
}
notifyAll();
}
}public synchronized void customer() throwsInterruptedException {while (true) {
System.out.println(Thread.currentThread().getName()+ ":::run:::" +productCount);
Thread.sleep(10);if (productCount <= 0) {
System.out.println("货舱已无货...无法消费");
wait();
}else{
productCount--;
}
notifyAll();
}
}
}