欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

线程安全的单例模式的几种实现方法分享

发布时间:2025/7/14 编程问答 45 豆豆
生活随笔 收集整理的这篇文章主要介绍了 线程安全的单例模式的几种实现方法分享 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

1、饿汉式单例

 

1 public class Singleton { 2 private final static Singleton INSTANCE = new Singleton(); 3 4 5 private Singleton() { } 6 7 public static Singleton getInstance() { 8 return INSTANCE; 9 } 10 }

 

2、借助内部类
属于懒汉式单例,因为Java机制规定,内部类SingletonHolder只有在getInstance()方法第一次调用的时候才会被加载(实现了lazy),而且其加载过程是线程安全的。内部类加载的时候实例化一次instance。

1 public class Singleton { 2 3 4 private Singleton() { } 5 6 7 private static class SingletonHolder { 8 private final static Singleton INSTANCE = new Singleton(); 9 } 10 11 public static Singleton getInstance() { 12 return SingletonHolder.INSTANCE; 13 } 14 }

3、普通加锁解决

1 public class Singleton { 2 3 4 private Singleton() { } 5 6 7 private static class SingletonHolder { 8 private final static Singleton INSTANCE = new Singleton(); 9 } 10 11 public static Singleton getInstance() { 12 return SingletonHolder.INSTANCE; 13 } 14 }

 

虽然解决了线程安全问题,但是每个线程调用getInstance都要加锁,我们想要只在第一次调用getInstance时加锁,请看下面的双重检测方案

4、双重检测,但要注意写法

public class Singleton {private static Singleton instance = null;private Singleton() { }public static Singleton getInstance() {if(instance == null) {synchronzied(Singleton.class) {Singleton temp = instance;if(temp == null) {temp = new Singleton();instance = temp}}}return instance;} }

 

由于指令重排序问题,所以不可以直接写成下面这样:

1 public class Singleton { 2 private static Singleton instance = null; 3 4 private Singleton() { } 5 6 public static Singleton getInstance() { 7 if(instance == null) { 8 synchronzied(Singleton.class) { 9 if(instance == null) { 10 instance = new Singleton(); 11 } 12 } 13 } 14 15 return instance; 16 } 17 }

 

但是如果instance实例变量用volatile修饰就可以了,volatile修饰的话就可以确保instance = new Singleton();对应的指令不会重排序,如下的单例代码也是线程安全的:

1 public class Singleton { 2 private static volatile Singleton instance = null; 3 4 private Singleton() { } 5 6 public static Singleton getInstance() { 7 if(instance == null) { 8 synchronzied(Singleton.class) { 9 if(instance == null) { 10 instance = new Singleton(); 11 } 12 } 13 } 14 15 return instance; 16 } 17 }

 

 

您可能感兴趣的文章:

转载于:https://www.cnblogs.com/blog-cq/p/5648856.html

总结

以上是生活随笔为你收集整理的线程安全的单例模式的几种实现方法分享的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。