[code]public class Flags2 {
  private boolean isReady = false;
  public synchronized void produce() {
    isReady = true;
    notifyAll();
  }
  public synchronized void consume() {
    while(!isReady) {
     try {
        wait();
       }catch(Exception ex) {}
    }
    isReay = true;
  }
}[/code][code]public class Flags2 {
  private boolean isReady = false;
  public synchronized void produce() {
    isReady = true;
    notifyAll();
  }
  public synchronized void consume() {
    while(!isReady) {
     try {
        wait();
       }catch(Exception ex) {}
    }
    isReay = false;
  }
}[/code]两段代码,最后一句 isReady = true; 或者 isReady = false;都可以编译,那么到底应该是isReady = ture还是isReady = false呢??为什么?

解决方案 »

  1.   

    目的是这个:in position so that the Flags2 class will compile and make appropriate use of the wait/notify mechanism.
    Note: You may reuse code elements.
      

  2.   

    我感觉是ture,多线程方面的问题吧.
    while(!isReady)就是要!isReady 为真,则isReady 为假,才进入循环,isReay = true;退出循环
      

  3.   


    false
    producer and consumer
    [code]public class Flags2 {
      private boolean isReady = false;
      public synchronized void produce() {
        isReady = true;                  //生产者产生食物
        notifyAll();                     //通知消费者消耗食物
      }
      public synchronized void consume() {
        while(!isReady) {                  //食物不存在
         try {
            wait();                       //消费者等待食物产生
           }catch(Exception ex) {}
        }
        isReay = false;                   //消费者消耗掉食物
      }
    }[/code