2015-04-29 67 views
0

我有一个剧本(线程)打印几件文本圆形的,并有一个screenbreak打印文本急,当screenbreak打印,它应该等待首先播放剧本。在屏幕截图打印所有文本后,它会通知剧本,并且剧本开始打印。java的,为什么thread.wait()没有在我的代码工作

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(); 

    } 
} 
+0

什么是你'等待的宗旨()'方法?它唯一能做的就是让你编写'screenplay.waits()'而不是写'screenplay.wait()'。这对于什么有所帮助?同上你的'notifys()'方法。 –

+0

@james large如何等待线程退出该线程?使用volatile变量,wait()为true时。有一个睡眠(),当时间很长时,紧急文本将不会立即打印。 – Ron

+0

@ jameslarge最重要的是它不会等着剧本。在等待之前有一个同步的单词,我认为它们之间可能有一些差异。 – Ron

回答

4

ScreenBreak开始wait() ING,没有人对notify()它。唯一致电notify()的号码是ScreenBreak,但它永远不会到达那里,因为它卡在wait()

建议:回tutorial

+0

你的意思是screenplay.waits()让screenbreak等待,但不是剧本。 – Ron

+1

好的,在技术上调用'wait()'使当前线程等待(在本例中是运行ScreenBreak的线程)。 – Kayaman

+0

非常感谢。你的答案非常棒。 – Ron

相关问题