首页 > 解决方案 > Java 线程 - 循环内的内容

问题描述

很简单。当我使用这样的方法运行时:

 @Override
 public void run() {
     CraftRoulette.getInstance();
     while(socket==null && outputstream==null && inputstream==null) {
        System.out.println("ahah");
     }
     System.out.println("RUN");
     while(socket!=null && !socket.isClosed()) {
        System.out.println("STOP");
        System.out.println("Checking");
        checkForUpdatesOnServer();
     }
     if(socket!=null && socket.isClosed()) System.out.println("FODA-SE, MAS QUE BEM!");
}

它输出:

 ahah
 (...)
 ahah
 RUN
 STOP
 Checking

但是,如果我删除第一个循环的内容:

 @Override
 public void run() {
 CraftRoulette.getInstance();
 while(socket==null && outputstream==null && inputstream==null) {
 }
 System.out.println("RUN");
 while(socket!=null && !socket.isClosed()) {
    System.out.println("STOP");
    System.out.println("Checking");
    checkForUpdatesOnServer();
 }
 if(socket!=null && socket.isClosed()) System.out.println("FODA-SE, MAS QUE BEM!");
}

它不输出任何东西。这是我应用的唯一更改。为什么让 sysout 让 run 方法继续运行而不是直接杀死它?

编辑:插座上的易失性完成了这项工作,谢谢!

标签: javamultithreadingwhile-loopconcurrencyrunnable

解决方案


推荐阅读