美文网首页
2020-12-11-Java-复习-61(面试题摘要6)

2020-12-11-Java-复习-61(面试题摘要6)

作者: 冰菓_ | 来源:发表于2020-12-22 08:07 被阅读0次

单例模式

public class Test2 {
    public static void main(String[] args) {
        People people1 = People.getInstance();
        People people2 = People.getInstance();
        System.out.println(people1 == people2);
        Singleton singleton1 = Singleton.getInstance();
        Singleton singleton2 = Singleton.getInstance();
        System.out.println(singleton1 == singleton2);
        User user1 = User.getInstance();
        User user2 = User.getInstance();
        System.out.println(user1 == user2);
        Student student1 = Student.getInstance();
        Student student2 = Student.getInstance();
        System.out.println(student1 == student2);
    }
}

//懒汉式
class People {
    private static People people = null;

    public synchronized static People getInstance() {
        if (people == null) {
            people = new People();
        }
        return people;

    }
}

//饿汉式,天生线程安全
class Singleton {
    private Singleton() {
    }

    private static Singleton single = new Singleton();

    //静态工厂方法
    public synchronized static Singleton getInstance() {
        return single;
    }
}

//双重检验,懒汉式
class User {
    private volatile static User user = new User();

    public static User getInstance() {
        if (user == null) {
            synchronized (User.class) {
                if (user == null) {
                    user = new User();
                }
            }
        }
        return user;
    }
}

//使用静态内部类
class Student {
    private static class StudentLazyHolder {
        private static final Student INSTANCE = new Student();
    }

    public static final Student getInstance() {
        return StudentLazyHolder.INSTANCE;
    }
}

分配内存空间
初始化对象
将对象指向刚分配的内存空间
但是有些编译器为了性能的原因,可能会将第二步和第三步进行重排序,顺序就成了
分配内存空间
将对象指向刚分配的内存空间
初始化对象
双重检验中不加volatile

image.png

相关文章

网友评论

      本文标题:2020-12-11-Java-复习-61(面试题摘要6)

      本文链接:https://www.haomeiwen.com/subject/yiklgktx.html