一、 什么是单例模式
1.概念:单例模式指的是在应用整个生命周期内只能存在一个实例。
a.对象如何产生? ----------------- 通过类的构造方法
b.如何限制对象的产生?----------- 通过构造方法限制
2.优点:能够避免实例对象的重复创建,减少创建实例的系统开销,节省内存
3.特点:1.单例类只能有一个实例。2.单例类必须自己自己创建自己的唯一实例。3.单例类必须给所有其他对象提供这一实例。
二、单例模式
1.饿汉式单例
所谓饿汉模式就是立即加载,也就是在类加载的时候已经产生了。这种方式适合占用资源少,在初始化的时候就会被用到的类。
//饿汉式单例
class Singlerton{
//1.构造方法私有化,类外部无法产生实例化对象
private Singlerton(){}
//2.类内部提供实例化对象,类外部无法改变,所有声明为全局常量
private static final Singlerton sg = new Singlerton();
//3.通过getter()方法取得类内部的私有化
public static Singlerton getSinglerton(){
return sg;
}
}
但是以上实现没有考虑线程安全问题。
所谓线程安全是指:如果代码所在的进程中有多个线程在同时运行,而这些线程可能会同时运行这段代码。如果每次运行结果和单线程运行的结果是一样的,而且其他的变量的值也和预期的是一样的,就是线程安全的。
显然以上实现并不满足线程安全的要求,在并发环境下很可能出现多个Singleton实例。
2.懒汉式单例:用时再new
懒汉模式就是延迟加载,也叫懒加载。在程序需要用到的时候再创建实例,这样保证了内存不会被浪费。
//懒汉式单例
//多线程下不安全:
class Singlerton2{
//1.构造方法私有化,类外部无妨访问,限制了对象的产生
private Singlerton2(){}
//2.类内部提供实例化对象,类外部无法改变,所有声明为全局常量
private static Singlerton2 sg2;
//3.通过getter方法取得类内部的私有化
public static Singlerton getSinglerton(){
if (sg2==null){
sg2 = new Singlerton2();
}
return sg2;
}
}
//多线程下安全:
class Singleton{
private Singleton(){}
private static volatile Singleton sin;
public static Singleton get(){
if(sin==null){
synchronized(Singleton.class){
if(sin==null){
sin = new Singleton();
}
}
}
return sin;
}
}
2.1 普通的懒汉式单例:单线程下没有问题,多线程下会出现问题
class Singlerton{
private static Singlerton sin;
private Singlerton(){}
public static Singlerton getSin(){
if(sin==null){
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
sin = new Singlerton();
}
return sin;
}
}
public class SinglertonTest {
public static void main(String[] args) {
//单线程下:
/**
System.out.println(Singlerton.getSin().hashCode());
System.out.println(Singlerton.getSin().hashCode());
System.out.println(Singlerton.getSin().hashCode());
System.out.println(Singlerton.getSin().hashCode());
System.out.println(Singlerton.getSin().hashCode());
System.out.println(Singlerton.getSin().hashCode());
System.out.println(Singlerton.getSin().hashCode());
System.out.println(Singlerton.getSin().hashCode());
System.out.println(Singlerton.getSin().hashCode());
*/
//多线程下:
for(int i=0; i<10; i++) {
new Thread(()-> {
System.out.println(Singlerton.getSin().hashCode());
}).start();
}
}
}
结果:单线程: 多线程:
举个栗子:现在有3个票贩子卖明星的演唱会门票,现在票贩子b和票贩子c手中的官网显示还有1张票,都想高价卖出去,现在票贩子c找到一个买家,同时b也找到一个买家,他们同时售卖,同时进行交易,就造成了1张票卖出去2次,这就是多线程下可能会出现的问题。
显然,对于上面写的单例,说明在多线程环境下,产生了多个对象,不符合单例模式的要求
2.2 双重加锁下的懒汉式单例:
class Singlerton{
private static Singlerton sin;
private Singlerton(){}
public static Singlerton getSin(){
if(sin==null){
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (Singlerton.class) {
if (sin == null) {
sin = new Singlerton();
}
}
}
return sin;
}
}
public class SinglertonTest {
public static void main(String[] args) {
//多线程
for(int i=0; i<10; i++) {
new Thread(()-> {
System.out.println(Singlerton.getSin().hashCode());
}).start();
}
//单线程
// System.out.println(Singlerton.getSin().hashCode());
// System.out.println(Singlerton.getSin().hashCode());
// System.out.println(Singlerton.getSin().hashCode());
// System.out.println(Singlerton.getSin().hashCode());
// System.out.println(Singlerton.getSin().hashCode());
// System.out.println(Singlerton.getSin().hashCode());
// System.out.println(Singlerton.getSin().hashCode());
// System.out.println(Singlerton.getSin().hashCode());
// System.out.println(Singlerton.getSin().hashCode());
}
}
结果: 多线程: 单线程:
举个栗子:现在有3个票贩子卖明星的演唱会门票,现在官网显示还有2张票但是官网有锁,一个时期只有一个人可以拥有钥匙,现在票贩子c、b、a都找到3个不同的买家,他们想要售卖,但是此时钥匙在票贩子a手中,a拿到钥匙,先上锁,保证只有自己进入官网,再进行判断,看官网是否还有票,如果有,就卖出。
这就是上面的双重加锁的懒汉式单例模式的类比,首先判断官网是否有票(if(sin==null)),确认有票后再看谁持有钥匙(synchronized(Singlerton.class)),持有钥匙的线程进入官网后再次判断官网的是否还有票(if(sin==null))。
3.饿汉式单例与懒汉式单例的区别
1)线程安全:
饿汉式天生就是线程安全的,可以直接用于多线程而不会出现问题。
懒汉式本身是非线程安全的,
2)资源加载和性能:
饿汉式在类创建的同时就实例化一个静态对象出来,不管之后会不会使用这个单例,都会占据一定的内存,但是相应的,在第一次调用时速度也会更快,因为其资源已经初始化完成。
而懒汉式顾名思义,会延迟加载,在第一次使用该单例的时候才会实例化对象出来,第一次调用时要做初始化,如果要做的工作比较多,性能上会有些延迟,之后就和饿汉式一样了。