单例模式
来源:互联网 发布:锐战网络 编辑:程序博客网 时间:2024/06/01 12:45
应用场景 :线程池 、数据库 连接池 缓存 硬件设备等
如果有多个实例 会有造成冲突 导致 结果不一致性等问题
public class Singleton {
private static Singleton uniqeInstance=null;
private Singleton(){
};
public static Singleton getInstance()
{
if(uniqeInstance==null)
{
uniqeInstance=new Singleton();
}
return uniqeInstance;
}
}
单例模式的优化
多线程优化 加入同步锁
2 双重锁检查
public class ChocolateFactory {
private boolean empty;
private boolean boiled;
public volatile static ChocolateFactory uniqueInstance = null;
private ChocolateFactory() {
empty = true;
boiled = false;
}
public static ChocolateFactory getInstance() {
if (uniqueInstance == null) {
synchronized (ChocolateFactory.class) {
if (uniqueInstance == null) {
uniqueInstance = new ChocolateFactory();
}
}
}
return uniqueInstance;
}
3 急切创建
public class Singleton {
private static Singleton uniqeInstance= new Singleton () ;
private Singleton(){
};
public static Singleton getInstance()
{
if(uniqeInstance==null)
{
uniqeInstance=new Singleton();
}
return uniqeInstance;
}
- 单例、单例模式
- 单例模式-多线程单例模式
- 单件模式(单例模式)
- 设计模式------单例模式
- 设计模式------单例模式
- 设计模式-单例模式
- 设计模式 - 单例模式
- 设计模式---单例模式
- 设计模式---单例模式
- PHP模式-单例模式
- 【设计模式】单例模式
- 设计模式-单例模式
- 设计模式----单例模式
- 设计模式--单例模式
- 设计模式-单例模式
- 单例模式(单子模式)
- 设计模式-单例模式
- [设计模式] 单例模式
- Gradle project sync failed. please fix your project and try again
- 9月7日学习日志
- Android
- 第一周- Java中String、StringBuffer和StringBuilder的区别
- ios 粘贴板
- 单例模式
- 单元测试说明
- HttpURLConnection
- oracle 中的 CONCAT,substring ,MINUS 用法
- C#调用C++动态库
- Catalogue/update in 2017.12.07
- 构建自定义的同步工具
- jedis在项目中的应用
- 2LIS_06_INV的初始化