public class Test {
public volatile int a =0;
public void add(){
this.a = 20;
}
}
import java.util.concurrent.TimeUnit;
public class Main {
public static void main(String[] args) {
Test test = new Test();
new Thread(()->{
System.out.println(Thread.currentThread().getName());
System.out.println("start");
try {
TimeUnit.SECONDS.sleep(10);
test.add();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("end "+test.a);
},"thread1").start();
while(test.a==0){
}
System.out.println(Thread.currentThread().getName()+" get value: "+test.a);
}
}