class Storage{   //ֿ
	private int count;
	private int size=50;
	private int i=0;
	public Storage()
	{	super();	}
	public synchronized void shengchan(String n){   //
		while (count == size){
			try {
				wait();
			}
			catch (InterruptedException e) {System.out.println("ж");}
		}		
		notify();
		count++;
		System.out.println(n+"ƷΪ: "+count);
	}
	public synchronized void xiaofei(String n){  //ѷ
		while (count == 0)	{	
try {   wait(); }
			catch (InterruptedException e) {System.out.println("ж"); }
		}
		i++;
		if(i>50)
			{System.exit(1);}
		notify();
		System.out.println(n+"ѲƷΪ: "+count);
		System.out.println("ѲƷ"+i+"");
		count--;
	}	
}
class Producer extends Thread{
	private String name;
	private Storage s;	
	public Producer(String n, Storage s){	
name = n;
		this.s = s;	}	
	public void run(){	
while (true){
	s.shengchan(name);
			try {	sleep((int)Math.random()*1000);}
			catch (InterruptedException e) {System.out.println("ж");} 
		}
	}
}
class Consumer extends Thread
{
	private String name;
	private Storage s;	
	public Consumer(String n, Storage s){	
name = n;
		this.s = s;	}	
	public void run(){
	while (true){
			s.xiaofei(name);			
			try { 	sleep((int)Math.random()*5000);}
			catch (InterruptedException e) {System.out.println("ж");} 
		}
	}
}
public class ShiXu2
{
	public static void main(String argv[]){
		Storage s = new Storage();
		Producer p1 = new Producer("", s);		
		Consumer c1 = new Consumer("", s);
		p1.start();		
		c1.start();
	  }
}
