public synchronized Bun pop(){
      if(index==0) {
        try{
           this.wait();
         } catch(InterruptedException e) {
            e.printStackTrace();
           } 
       }
       this.notify();
        index--;      
        return arr[index];
  }
                             public synchronized Bun pop(){
      while(index==0) {
        try{
           this.wait();
         } catch(InterruptedException e) {
            e.printStackTrace();
           } 
       }
       this.notify();
        index--;      
        return arr[index];
  }   他们说用if会不安全,不过我怎么感觉他们都是一样啊,不都是抛出异常时跳出去了吗?

解决方案 »

  1.   

    呵呵,不好意思啊,我没有把全部的拷进来,是这样的:我是说当程序线程进入循环以后,一般情况下都可以正常运行,但老师说,如果WAIT()抛出异常时,用IF的那个就会不安全,它们抓住InterruptException后,继续执行后面的,但WHILE语句它不会。
      

  2.   

    这里wait()抛出异常时,用while的话是不断的调用wait()直到wait成功.如果是if的话直接继续执行后面的.