在while循环中更改条件不会生效

时间:2017-10-30 02:22:32

标签: java while-loop conditional-statements

昨天,我写了一些这样的代码:

public class WhileTest {
private static boolean close=false;
public static void main(String[] args) throws InterruptedException {
    new Thread(new Runnable() {

        @Override
        public void run() {
            while(true) {
                int i=0;
                while(!close) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("count:"+i++);
                }
            }
        }
    }).start();

    System.out.println("close:"+close);
    Thread.sleep(3000);
    close=true;
    System.out.println("close:"+close);
    Thread.sleep(3000);
    close=false;
    System.out.println("close:"+close);
    Thread.sleep(3000);
    close=true;
    System.out.println("close:"+close);
    System.exit(0);
}}

当close再次设置为false时,它不再是打印计数了,我运行javap看看while循环中的关闭条件是否编译为true,而不是,我不明白,任何人都可以帮助我吗? / p>

0 个答案:

没有答案