`
sunxboy
  • 浏览: 2828367 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

使用双重锁判定可以大幅降低锁的征用

 
阅读更多
class ObjInstance {
//单例
private static ObjInstance oi = new ObjInstance();
private static User user;
private ObjInstance() {
}
public static User getUserInstance() {
if (user == null) {// 无锁判定
synchronized (oi) {
if (user == null) {// 加锁判定
user = new User(1, "zs");
}
}
}
return user;
}
}

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics