单例模式
来源:互联网 发布:淘宝宝贝详情如何添加 编辑:程序博客网 时间:2024/06/15 06:13
1、单例模式双重校验
public class 单例模式双重校验锁 { private volatile static 单例模式双重校验锁 instance; private 单例模式双重校验锁(){ //防止构造器实例化 } public static 单例模式双重校验锁 getInstance() { if (instance == null) { synchronized (单例模式双重校验锁.class){ if (instance == null) { instance = new 单例模式双重校验锁(); } } } return instance; } public static void main(String[] args) { System.out.println(getInstance()); System.out.println(getInstance()); }}
2、单例之懒汉模式
public class 单例模式懒汉模式 { private 单例模式懒汉模式(){} private static 单例模式懒汉模式 instance = null; public static synchronized 单例模式懒汉模式 getInstance() { if (instance == null) { instance = new 单例模式懒汉模式(); } return instance; } public static void main(String[] args) { System.out.println(getInstance()); System.out.println(getInstance()); }}3、单例之饿汉模式public class 单例模式饿汉模式 { private static 单例模式饿汉模式 instance = null; static { instance = new 单例模式饿汉模式(); } private 单例模式饿汉模式(){ } public static 单例模式饿汉模式 getInstance() { return instance; } public static void main(String[] args) { System.out.println(单例模式饿汉模式.getInstance()); System.out.println(单例模式饿汉模式.getInstance()); System.out.println(单例模式饿汉模式.getInstance()); }}4、单例之静态内部类public class Main { private static class Singleton{ private static final Main instance = new Main(); } private Main(){ } public static Main getSingleton() { return Singleton.instance; } public static void main(String[] args) { System.out.println(Main.getSingleton()); System.out.println(Main.getSingleton()); }}
阅读全文
0 0
- 单例、单例模式
- 单例模式-多线程单例模式
- 单件模式(单例模式)
- 设计模式------单例模式
- 设计模式------单例模式
- 设计模式-单例模式
- 设计模式 - 单例模式
- 设计模式---单例模式
- 设计模式---单例模式
- PHP模式-单例模式
- 【设计模式】单例模式
- 设计模式-单例模式
- 设计模式----单例模式
- 设计模式--单例模式
- 设计模式-单例模式
- 单例模式(单子模式)
- 设计模式-单例模式
- [设计模式] 单例模式
- hdoj 3652 B-number
- STL-stack实现栈
- Jmeter接口测试-HTTP请求
- Android消息机制及Handler与Looper和Message的关系
- zipimport.ZipImportError: can't decompress data; zlib not available
- 单例模式
- angular-validation校验数字
- PDO事务实例
- 哥德巴赫猜想
- Groovy 基本类型
- 加权随机数
- 安装pip最简单的方法
- 【HDU OJ 1068】Girls and Boys
- Libevent简介