class Computer{
int num=0;
boolean flag=false;
public synchronized void set(){
if(flag){
try{
super.wait();
}catch(InterruptedException e){
e.printStackTrace();
}
}
try{
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
num++;
System.out.println("生产了第"+num+"台电脑。");
flag=true;
super.notify();
}
public synchronized void get(){
if(!flag){
try{
super.wait();
}catch(InterruptedException e){
e.printStackTrace();
}
}
try{
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
System.out.println("取走了第"+num+"台电脑。");
flag=false;
super.notify();
}
}
class Customer implements Runnable{
private Computer com;
public void setCompu(Computer compu){
this.com=compu;
}
public Computer getCompu(){
return this.com;
}
public Customer(Computer com){
this.com=com;
}
public void run(){
for(int i=0;i<10;i++){
com.set();
com.get();
}
}
}
public class Test9_2 {
public static void main(String[] args) {
Computer a=new Computer();
Customer cus=new Customer(a);
new Thread(cus).start();
}
}