package com.aoptest.test;
public class ProblemDemo {
/*volatile*/ boolean flag = true;
void test() {
System.out.println("start");
while (flag) {
System.out.println(".");
}
System.out.println("end");
}
public static void main(String[] args) {
ProblemDemo problemDemo = new ProblemDemo();
new Thread(problemDemo::test).start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
problemDemo.flag = false;
}
}
另外去掉while的循环体“System.out.println(".");”,结果很费解,sout为什么会影响结果呢