0

以下代码正常工作,即打印Hello!,直到我按enter。当我按下输入键时,它停止。如预期的那样,这很好。线程基础知识:为什么相同的代码在不同的类中产生不同的输出?

但是,当我的相同的代码复制到名为OneDataCached另一个类(所以它是不同的权只是类的名称),它并不会达到预期效果。它,而打印Hello!,当我按下enter,它忽略了一个线,然后再次开始打印hello!

我感到困惑,为什么发生这种情况,任何提示或指针将不胜感激。

public class One { 
    static volatile boolean bool=true; 

    void stopThread(){ 
     bool=false; 
    } 

    public static void main(String [] args){ 

     new Thread(new Runnable() { 
      public void run(){ 
       while(bool){ 
        System.out.println("Hello!"); 
        try { 
         Thread.sleep(1000); 
        } catch (InterruptedException e) { 
         e.printStackTrace(); 
        } 
       } 
      } 
     }).start(); 

     System.out.println("Press Return to stop ..."); 

     new Scanner(System.in).nextLine(); 
     new One().stopThread(); 

    } 

} 

enter image description here

复制后的代码OneDateCached CLASS: -

enter image description here


编辑: -

public class OneDataCached { 
    static volatile boolean bool=true; 

    void stopThread(){ 
     OneDataCached.bool=false; 
    } 

    public static void main(String [] args){ 

     new Thread(new Runnable() { 
      public void run(){ 
       while(OneDataCached.bool){ 
        System.out.println("Hello!"); 
        try { 
         Thread.sleep(1000); 
        } catch (InterruptedException e) { 
         e.printStackTrace(); 
        } 
       } 
      } 
     }).start(); 

     System.out.println("Press Return to stop ..."); 

     new Scanner(System.in).nextLine(); 
     new One().stopThread(); 

    } 

} 
+2

您是否使用'OneDataCached.bool'或'One.bool'? '静态'这种方式很有趣 – MadProgrammer

+0

不同的'布尔'。 –

+0

@MadProgrammer为了检查,我改变了'while'循环的条件以及'stopThread()'中的'bool'到'OneDataCached.bool';但是这没有帮助。虽然我认为你的观点非常合理。我编辑了这个问题。 – Solace

回答

1

也许你正在呼吁

new One().stopThread(); 

,而不是

new OneDataCached().stopThread(); 
+0

非常感谢。当我复制时,我忘了更改它。这工作。 – Solace

1

假设您已经逐字复制的代码,那么这两个版本将被调用

new One().stopThread(); 

这意味着OneDataCached.bool是从来没有改变过。 static是不是你的朋友,并会引发各种问题,如果你不警惕呢?

一种解决方案是创建一个特殊Runnable这是自包含...

public class Message implements Runnable { 

    private volatile boolean bool = true; 

    void stopThread() { 
     bool = false; 
    } 

    @Override 
    public void run() { 
     while (bool) { 
      System.out.println("Hello!"); 
      try { 
       Thread.sleep(1000); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

而且那么只有曾经与你想停止/修改,例如实例交互...

public class TestMessage { 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 

     Message message = new Message(); 
     Thread t = new Thread(message); 
     t.start(); 

     System.out.println("Press Return to stop ..."); 

     new Scanner(System.in).nextLine(); 
     message.stopThread(); 
    } 
} 
+0

非常感谢您的解决方案。它澄清了我的概念。 – Solace

1

如果

new Thread(new Runnable() { 
     public void run(){ 
      while(bool){ 
       System.out.println("Hello!"); 
       try { 
        Thread.sleep(1000); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
    }).start(); 

One中声明,名称bool是指在One中声明的static字段。如果在类SomeOther中声明了相同的代码,则它指的是在SomeOther中声明的static字段。

但是,new One().stopThread();将始终修改您的线程未查看的One中声明的字段。

相关问题