单例设计模式
来源:互联网 发布:windows ntp配置 编辑:程序博客网 时间:2024/05/06 01:27
听到单例设计模式大家可能会想到最多的是懒汉式和饿汉式吧,这里这两种方式我都不介绍了,可以参考我的这篇文章饿汉式
我要介绍的是在分布式中常用的,inner in class的这种形式(内部类的形式)(其实还是一样的原理)
public class InnerSingleton { private static class Singletion { private static Singletion single = new Singletion(); } public static Singletion getInstance(){ return Singletion.single; }}
dobbleSingleton
public class DubbleSingleton { private static DubbleSingleton ds; public static DubbleSingleton getDs(){ if(ds == null){ try { //模拟初始化对象的准备时间... Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (DubbleSingleton.class) { if(ds == null){ ds = new DubbleSingleton(); } } } return ds; } public static void main(String[] args) { Thread t1 = new Thread(new Runnable() { @Override public void run() { System.out.println(DubbleSingleton.getDs().hashCode()); } },"t1"); Thread t2 = new Thread(new Runnable() { @Override public void run() { System.out.println(DubbleSingleton.getDs().hashCode()); } },"t2"); Thread t3 = new Thread(new Runnable() { @Override public void run() { System.out.println(DubbleSingleton.getDs().hashCode()); } },"t3"); t1.start(); t2.start(); t3.start(); }}
阅读全文
0 0
- 设计模式--单例
- 单例设计模式
- 设计模式----单例
- 单例设计模式
- 单例设计模式
- 单例设计模式
- 单例设计模式
- 单例设计模式
- 单例设计模式
- 单例设计模式
- 设计模式-单例
- 单例设计模式
- 单例设计模式
- 单例设计模式
- 单例设计模式
- 单例设计模式
- 单例设计模式
- 单例设计模式
- prompt弹框的使用
- static(静态)变量有什么作用?
- HDU 5793 A Boring Question (快速幂 + 乘法逆元 + 费马小定理)
- UVA 1599 理想路径
- VS2010中“Stack around the variable 'XXX' was corrupted.”处理方法
- 单例设计模式
- java并发编程实战-取消与关闭2
- 23th 【树状数组】树状数组1&&2
- linux内核深入学习参考
- 草根学Python(八) 模块与包
- sublime安装方式及快捷键
- myeclipse的svn插件解决冲突的几种情况。
- 操作系统复习-4.2文件系统实现
- C++ 变量初始化顺序