日期:2014-05-20  浏览次数:20612 次

帮忙看一段程序,关于线程的
//Sender.java
class   Buffer
{
private   int   value;
private   boolean   isEmpty=true;


synchronized   void   put(int   i)
{
          while(!isEmpty)
              {
                    try
                      {
                            this.wait();
                      }
                    catch(InterruptedException   e)
                      {
                            System.out.println(e.getMessage());
                      }
              }
          value=i;
          isEmpty=false;
        notify();
}


synchronized   int   get()
{
          while(isEmpty)
              {
                    try
                      {
                            this.wait();
                      }
                    catch(InterruptedException   e)
                      {
                            System.out.println(e.getMessage());
                      }
              }
          isEmpty=true;
          notify();
          return   value;
}
}


class   Sender   extends   Thread
{
private   Buffer   bf;


public   Sender(Buffer   bf)
{
        this.bf=bf;
}
public   void   run()
{
        for(int   i=1;i <6;i++)
            {
                bf.put(i);
                System.out.println( "Sender   put "+i);
            }
}

public   static   void   main(String   args[])
{
          Buffer   bbb=new   Buffer();
          (new   Sender(bbb)).start();
          (new   Receiver(bbb)).start();
}
}


class   Receiver   extends   Thread
{
private   Buffer   bf;

public   Receiver(Buffer   bf)
{