我是靠谱客的博主 端庄小猫咪,这篇文章主要介绍设计模式之单例模式概念篇概念 ,现在分享给大家,希望可以做个参考。

概念

单例模式可以说是设计模式里面最好理解的一个模式了,它的意思就是一个类只创建一个对象,所有的引用都只向该对象去操作。

单例模式满足的要求:

1.构造函数私有化

2.类的内部创建实例

3.提供静态的唯一获取实例的方法

单例模式之饿汉式

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@Data public class Singleton { private static Singleton singleton=new Singleton(); //内部创建实例 private Integer data=0; //成员变量 private Singleton(){} //构造函数私有化 //本身线程安全 public static Singleton getTarget(){ return singleton; } }

单例模式之懒汉式

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@Data public class Singleton { private static Singleton singleton=null; //内部创建实例 private Integer data=0; //成员变量 private Singleton(){} //构造函数私有化 //使用同步方法保证线程安全 public static synchronized Singleton getTarget(){ if (singleton==null) { singleton = new Singleton(); } return singleton; } }

单例模式之双重检测懒汉式(DCL)

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
@Data public class Singleton { private volatile static Singleton singleton=null; //内部创建实例,并使用volatile修饰保证其可见性 private Integer data=0; //成员变量   //构造函数私有化 private Singleton(){}   //对外提供获取实例方法,并且通关双重判空,为什么要使用双重判空而不是单重?   //1.外判内不判:如果同时两个线程先判空进入方法内后,一个线程拿到锁进去   //创建对象,另一个线程再拿到锁进去又创建对象   //2.内判外不判:外判可以先判是否有对象,不需要进去同步代码块提高效率   //同步代码块加锁的方式保证线程安全,只创建出一个实例,对比懒汉式效率更高 public static Singleton getTarget(){ if (singleton==null){ synchronized (Singleton.class){ if (singleton==null){ singleton=new Singleton(); } } } return singleton; } }

单例模式之内部类

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@Data public class Singleton { private Integer data=0; //成员变量 private Singleton(){} //构造函数私有化 //静态内部类 private static class Target{ private static final Singleton singleton = new Singleton();//内部类创建实例 } //当任何一个线程第一次调用getTarget()时,都会使Target被 //加载和被初始化,此时静态初始化器将执行Singleton的初始化操作。 // (被调用时才进行初始化!)初始化静态数据时,Java提供了的线程安全性保证。 public static final Singleton getTarget(){ return Target.singleton; } }

单例模式之枚举

public enum Singleton {
   JAVA_Singleton,
}

//简单,安全,即使是在面对复杂的序列化或者反射攻击的时候

单例是线程安全的吗?

并不是,虽然说对于创建单例,我们有很多办法去保证其只创建出一个实例(线程安全),但是,假如多个线程来操作该单例对象,该单例对象就相当于一个共享变量,所以并不是线程安全的,如下图,应该是2000000。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class SingletonThread implements Runnable{ @Override public void run() { for (int i=0;i<1000000;i++){ Singleton singleton = Singleton.getTarget(); singleton.setData(singleton.getData()+1); System.out.println(Thread.currentThread().getName()+":"+singleton.getData()); } } }
复制代码
1
2
3
4
5
6
7
8
9
10
@GetMapping("/singletonDemo") public String singletonDemo(){ SingletonThread run=new SingletonThread(); Thread thread1 =new Thread(run); Thread thread2 =new Thread(run); thread1.start(); thread2.start(); return ReturnJson.success(); } }

解决:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class SingletonThread implements Runnable{ private static final Lock lock=new ReentrantLock(); @Override public void run() { for (int i=0;i<1000000;i++){ try { lock.lock(); Singleton singleton = Singleton.getTarget(); singleton.setData(singleton.getData()+1); System.out.println(Thread.currentThread().getName()+":"+singleton.getData()); }catch (Exception e){ System.out.println(e); }finally { lock.unlock(); } } } }

 

最后

以上就是端庄小猫咪最近收集整理的关于设计模式之单例模式概念篇概念 的全部内容,更多相关设计模式之单例模式概念篇概念 内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(69)

评论列表共有 0 条评论

立即
投稿
返回
顶部