单例模式的几种形式
来源:互联网 发布:印尼旅行社软件 编辑:程序博客网 时间:2024/06/06 00:00
public class Singleton{//饿汉模式,线程安全,但是就算不调用get也会创建新对象 private static final singleton = new Singleton; private Singleton(){ } public static Singleton getSingleton(){ return singleton; }}public class Singleton{//懒汉模式,但是线程不安全 private static singleton ; private Singleton(){ } public static Singleton getSingleton(){ if(singleton == null){ singleton = new Singleton(); } return singleton; }}public class Singleton{//线程安全,但是效率低 private static singleton; private Singleton(){ } public static synchronized Singleton getSinglet(){ if(singleton == null){ singleton = new Singleton(); } return singleton; }}public class Singleton{//双重检验锁。 private volatile static singleton; private Singleton(){ } public static Singleton getSingleton(){ if(singleton == null){ synchronized(Singleton.class){ if(Singleton == null){ singleton = new Singleton(); } } } retuen singleton }}// 枚举public enum Singleton {//最右bige和简单的方法 INSTANCE; // 自定义的其他任意方法 public void whateverMethod() { }}public class Singleton {//静态内部类,防止不兼容 private static class SingletonHolder { private static final Singleton singleton = new Singleton(); } private Singleton() { } public static Singleton getSingleton() { return SingletonHolder.singleton; }}
阅读全文
0 0
- 单例模式的几种形式
- swift 单例的几种形式
- 单例的几种形式
- 单例模式的三种形式
- 单例形式模式
- 单例模式的三种常用的形式
- java单例模式的两种形式
- 单例的两种实现形式模式
- 初学者理解单例模式的2种形式
- 单例模式的五种实现形式
- Double Check形式的单例模式
- 单例模式的最高级形式
- 单例模式的其他实现形式
- Singleton模式的几种形式
- 单例模式的几种实现
- 单例模式的几种写法
- 单例模式的几种写法
- 单例模式的几种写法
- C++实现快速排序(源代码) 标签: c++算法systemc 2011-11-09 22:01 75011人阅读 评论(11) 收藏 举报 分类: Algorithm(1) C/C++(13
- Junit 单元测试 Spring 读取WEB-INF下 xml 文件
- [leetcode]: 537. Complex Number Multiplication
- Hadoop 不需认证导致数据泄漏
- MNIST手写数字识别
- 单例模式的几种形式
- 在Android Manifest中出现Use '$' instead of '.' for inner classes (or use only lowercase letters in pac
- Portal for arcgis 服务器集成方式区别
- 自写简单下拉列表
- 游标什么时候消失
- sql 取年份后两位和月份
- 高斯噪声/白噪声/高斯白噪声的区别
- jobcontrol
- 第一个Struts2项目(下)