volatile关键字之可见性使用
package com.yifan.jvm.jmm;
public class Memory {
volatile int count = 0;
void addCOunt(){
this.count = 10;
}
public static void main(String[] args) {
Memory memory = new Memory();
new Thread(()->{
System.out.println(Thread.currentThread().getName()+"进来!!!");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
memory.addCOunt();
System.out.println(Thread.currentThread().getName()+"更改count值:"+memory.count);
},"线程一").start();
while (memory.count == 0){
}
System.out.println("main线程结束~~~~~~~~~~~~~~"+memory.count);
}
}