单例模式线程安全问题

package com.cj;

class Book {
    private Book(){}
    private static Book instance = null;

    public static Book getInstance() {
        if(instance==null){
            //同步代码块
            synchronized (Book.class){ 
                if(instance==null){
                    instance = new Book();
                }
            }
        }
        return instance;
    }
}

 

posted @ 2022-04-02 17:38  写代码的小哥哥  阅读(24)  评论(0)    收藏  举报