单实例模式
来源:互联网 发布:电脑上录歌用什么软件 编辑:程序博客网 时间:2024/04/28 07:41
1. 经典的单实例模式例子(非线程安全):
public class Singleton {
private static Singleton uniqueInstance;
// other useful instance variables here
private Singleton() {}
public static Singleton getInstance() {
if (uniqueInstance == null) {
uniqueInstance = new Singleton();
}
return uniqueInstance;
}
// other useful methods here
}
本例是最经典的单实例模式例子,但是在多线程的情况下就会产生多个实例!
2. 线程安全的例子:
public class Singleton {
private static Singleton uniqueInstance;
// other useful instance variables here
private Singleton() {}
public static synchronized Singleton getInstance() {
if (uniqueInstance == null) {
uniqueInstance = new Singleton();
}
return uniqueInstance;
}
// other useful methods here
}
增加synchronized,会让该方法是线程安全的,但是会引起每个线程在调用该方法时的等待,如果getInstance的性能对应用程序不是很关键(记住,同步方法可能会使getInstance方法得运行效率降低100倍),本方法是最好得方法!
3. 提前实例化,不适用延迟实例化(使用于创建和运行时负担不太繁重或者应用程序总是创建并使用单件实例),它是线程安全的:
public class Singleton {
private static Singleton uniqueInstance = new Singleton();
// other useful instance variables here
private Singleton() {}
public static Singleton getInstance() {
return uniqueInstance;
}
// other useful methods here
}
采用这种方法,我们依赖JVM在加载这个类时候马上创建此唯一实例,JVM保证在任何线程访问它之前,一定先创建它!
4. 在java1.5及以后的版本,增加了volatile关键字,可以采用双重检查加锁!
public class Singleton {
private volatile static Singleton uniqueInstance;
private Singleton() {}
public static Singleton getInstance() {
if (uniqueInstance == null) {
synchronized (Singleton.class) {
if (uniqueInstance == null) {
uniqueInstance = new Singleton();
}
}
}
return uniqueInstance;
}
}
volatile关键字确保:当unigueInstance变量在被初始化成实例时,多个线程能够正确的处理它!(对于关注性能的程序,这种做法可以大大减少时耗)
- 单实例模式(Singleton)
- 单实例的模式
- C#单实例模式
- 单实例模式
- 单实例模式
- 单例模式 实例
- 设计模式 - 单实例
- 单例模式实例
- C++单实例模式
- 单例模式实例
- 单实例设计模式
- 单例模式实例
- 单实例模式 加锁
- 单例模式实例
- 单例模式实例
- 设计模式--单实例模式
- C# 单实例设计模式
- C# 实例 -------单例模式
- CTreeCtrl二三
- 从源码编译skyeye的mmi_branch
- Java Stack System Monitor
- C#中的多维数组与C中的多维数组的区别
- linux开发所需知识收集整理(新人起步阶段)
- 单实例模式
- GDP
- 软件测试经理访谈:软件测试行业发展及软件测试人才外包
- Flex 经典例子: 建立Restaurant Finder
- GridView 单击选择行,双击打开详细页面,鼠标移到某行上变色
- Flex RemoteObject类参考
- C# 中的委托和事件
- Calling RemoteObject components in ActionScript
- 解决不能通过mysql.sock连接MySQL问题