单实例模式

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变量在被初始化成实例时,多个线程能够正确的处理它!(对于关注性能的程序,这种做法可以大大减少时耗)

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值