package demo03;
public class Storage {
private final static int MAX_NUM=1000;//仓库最大的储存数量
private int store;//目前仓库存储的数量
public Storage(int store){
this.store=store;
}
//入库操作
public synchronized void in(int num){
String threadName=Thread.currentThread().getName();
if(this.store+num>MAX_NUM){
System.out.println(threadName+"被挂起!");
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
this.store+=num;//入库
this.notifyAll();//唤醒等待线程
System.out.println(threadName+"生成产品数量为:"+num+" 仓库现在的数量为:"+this.store);
}
//出库操作
public synchronized void out(int num){
String threadName=Thread.currentThread().getName();
if(this.store<num){ system.out.println(threadname+"被挂起!");="" try="" {="" this.wait();="" }="" catch="" (interruptedexception="" e)="" todo="" auto-generated="" block="" e.printstacktrace();="" this.store-="num;//出库" this.notify();="" 唤醒等待的线程="" system.out.println(threadname+"消费产品数量为:"+num+"="" 仓库现在的数量为:"+this.store);="" }<="" pre=""></num){>
|