使用AtomicBoolean和synchronized实现只初始化一次的操作

public class Test {
    private AtomicBoolean atomicBoolean = new AtomicBoolean(false);
    private volatile boolean isInit = false;

    //输出:
    //1:init
    //2:init
    public void init(int i){
        if (!isInit) {
            sleep();
            isInit = true;
            System.out.println(i + "init");
            return;
        }
        System.out.println(i + ":not init");
    }

    //输出:
    //2:not init
    //1:init
    public void initAB(int i){
        if(atomicBoolean.compareAndSet(false, true)) {
            sleep();
            System.out.println(i + ":init");
            return;
        }
        System.out.println(i + ":not init");
    }

    //输出:
    //1:init
    //2:not init
    public void initS(int i){
        if (!isInit){
            sleep();
            synchronized (this){
                if (!isInit){//如果isInit不是volatile的话,虽然线程1赋值isInit为true了,但不一定能及时更新到主内存中,这样线程2获取时可能还是false!
                    isInit = true;
                    System.out.println(i + "init");
                    return;
                }
                System.out.println(i + ":not init");
            }
        }
    }

    public static void main(String[] args) {
        Test t = new Test();
        new Thread(){
            @Override
            public void run() {
                t.init(1);
                //t.initAB(1);
                //t.initS(1);
            }
        }.start();
        new Thread(){
            @Override
            public void run() {
                t.init(2);
                //t.initAB(2);
                //t.initS(2);
            }
        }.start();
    }

    private void sleep(){
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

你可能感兴趣的:(并发)