请大家帮帮我理解这个程序中,notify()和wait()的用法!!
class lianxi
{
public static void main(String []args)
{
Queue q=new Queue();
Producer p=new Producer(q);
Consumer c=new Consumer(q);
p.start();
c.start();
}
}
class Producer extends Thread
{
Queue q;
Producer(Queue q)
{
this.q=q;
}
public void run()
{
for(int i=0;i<10;i++)
{
q.put(i);
System.out.println("producer put"+i);
}
}
}
class Consumer extends Thread
{
Queue q;
Consumer(Queue q)
{
this.q=q;
}
public void run()
{
while(true)
{
System.out.println("Consumer get"+q.get());
}
}
}
class Queue
{
int value;
boolean bfull=false;
public synchronized void put(int i)
{
if(!bfull)
{
value=i;
bfull=true;
notify();
try
{
wait();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
public synchronized int get()
{
if(!bfull)
{
try
{
wait();
}
catch(Exception e)
{
e.printStackTrace();
}
}
bfull=false;
notify();
return value;
}
}