volatile保证可见性

package pj.imcp;

public class Main3 {
    static  boolean  a = false;
//    static  volatile boolean  a = false;

    public static void main(String[] args) throws InterruptedException {
        Thread.currentThread().yield();
        Thread.sleep(3);
        Thread.yield();

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (!a){

                }
                System.out.println("out___________________");
            }
        }).start();

//        Thread.sleep(3);
        System.out.println("----***************----------");


        a = true;

    }

}
volatile 保证可见性
分别去掉注解可发现不同结果

发表回复