public class HelloVolatile {
private static boolean running = true;
private static void test() {
System.out.println("start ");
while (running) {
}
System.out.println("end ");
}
public static void main(String[] args) throws InterruptedException {
new Thread(HelloVolatile::test, "t1").start();
Thread.sleep(1000);
running = false;
}
}
输出结果如下
start
end
running = false 不是在main线程修改的吗,为什么影响到了t1子线程输出了end语句,end不是应该不输出吗??
网上还有说在while中如果加了println会影响,我这也没加
针对非static变量兄嘚