单例定义

一个类只有一个实例,并提供一个全局访问点。

巧妙利用了编程语言的一些语法规则:构造函数private, 然后提供一个public的方法返回类的一个实例;又方法和返回的类的实例都是static类型,所以只能被类所拥有,而不能被实例化类的对象拥有。这样一个类就只能有一个实例了。

  1.  最简单的写法(非线程安全,有叫它“懒汉式”的)
public class Singleton {
    private static Singleton uniqueInstance;
    private Singleton() {}

    public static Singleton getInstatnce() {
        if (uniqueInstance == null) {
            uniqueInstance = new Singleton();
        }
        return uniqueInstance;
    }
}

   2. 加”synchronized“保证多线程下的线程安全(同步代码块,高频访问时,性能较差)

public class Singleton {
    private static Singleton uniqueInstance;
    private Singleton() {}

    public static synchronized Singleton getInstatnce() {
        if (uniqueInstance == null) {
            uniqueInstance = new Singleton();
        }
        return uniqueInstance;
    }
}

  3. ”急切“或”饿汉“式(线程安全,因为JVM加载此类时立即创建此类的唯一实例)

public class Singleton {
    private static Singleton uniqueInstance = new Singleton();
    private Singleton() {}

    public static Singleton getInstatnce() {
        return uniqueInstance;
    }
}
View Code

相关文章:

  • 2021-12-09
  • 2021-07-31
  • 2022-02-18
  • 2021-06-26
猜你喜欢
  • 2021-10-29
  • 2021-11-26
  • 2021-12-23
  • 2021-11-04
  • 2021-04-26
相关资源
相似解决方案