java,为什么 thread.wait() 在我的代码中不起作用

java, why thread.wait() don't work in my code

我有一个screenplay(Thread)循环打印几段文字,并且有一个screenbreak打印紧急文本,当screenbreak打印时,它应该先等待screenplay。截屏打印所有文字后,会通知剧本,剧本开始打印。

class ScreenPlay implements Runnable{

    public synchronized void notifys() throws InterruptedException {
      notify();
    }

    public synchronized void waits() throws InterruptedException {
      wait();
    }

    public void run(){
      for(int i=0; i<15; i++){   
          System.out.println(i);
          try{
            Thread.sleep(500); 
          }catch(InterruptedException e){
            e.printStackTrace();
          }
          if( i == 14 ){
              i = -1;
          }
      }
    }
}

class ScreenBreak implements Runnable{
  private ScreenPlay screenplay;

  public ScreenBreak(ScreenPlay screenplay){
    this.screenplay = screenplay;
  }

  public void run(){
    try{
      Thread.sleep(2000);
      screenplay.waits();
    }catch(InterruptedException e){
      e.printStackTrace();
    }
    for(int i=0; i<5; i++){
      System.out.println("@_" + i);
    }
    try{
      Thread.sleep(5000);
      screenplay.notifys();
    }catch(InterruptedException e){
      e.printStackTrace();
    }
  }

}

public class Waits {
    public static void main(String[] args) {

      ScreenPlay s = new ScreenPlay();
      ScreenBreak sb = new ScreenBreak(s);
      new Thread(s).start();
      new Thread(sb).start();

    }
}

输出显示 'wait()' 根本不起作用,剧本继续打印。 screenbreak 从不打印其文本。 为什么?这里出了什么问题?

我修改了代码,它起作用了。

class ScreenPlay implements Runnable{

    private int waittime = 1050;
    private boolean isPause = false;

    public synchronized  void setIsPause(boolean isPause){
      this.isPause = isPause;
    }

    public synchronized void go() throws InterruptedException {
      this.isPause = false;
      notify();
    }

    public void run(){
      for(int i=0; i<15; i++){   
          System.out.println(i);
          try{
            for(int j = 0; j < waittime/500 + 1; j++){
              Thread.sleep(500); 
              synchronized(this){
                if(isPause){
                  System.out.println("waiting");
                 wait();
                }else{
                  System.out.println("..."); 
                }
              }
            }
          }catch(InterruptedException e){
            e.printStackTrace();
          }
          if( i == 14 ){
              i = -1;
          }
      }
    }
}

class ScreenBreak implements Runnable{
  private ScreenPlay screenplay;

  public ScreenBreak(ScreenPlay screenplay){
    this.screenplay = screenplay;
  }

  public void run(){
    try{
      Thread.sleep(2000);
    }catch(InterruptedException e){
      e.printStackTrace();
    } 
    screenplay.setIsPause(true);
    try{
      Thread.sleep(5000);
      for(int i=0; i<5; i++){
          System.out.println("@_" + i);
      }
      screenplay.go();
    }catch(InterruptedException e){
      e.printStackTrace();
    }

  }

}

public class Waits {
    public static void main(String[] args) {

      ScreenPlay s = new ScreenPlay();
      ScreenBreak sb = new ScreenBreak(s);
      new Thread(s).start();
      new Thread(sb).start();
      try{
        Thread.sleep(15000);
      }catch(InterruptedException e){
        e.printStackTrace();
      }
      new Thread(sb).start();

    }
}

ScreenBreak 开始 wait()ing 时,没有人 notify() 它。对 notify() 的唯一调用是在 ScreenBreak,但它永远不会到达那里,因为它卡在 wait()

建议:回tutorial