public class MyObject {
    
    private volatile static MyObject instance;
    
    private MyObject(){}
    
    public static MyObject getInstance(){
        try {
            if(instance != null){
                return instance;
            }else{synchronized ( instance.class) {
                    if(instance == null){
                        instance = new MyObject();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return instance;
    }

}

 

相关文章: