首页 > 编程语言 > 详细

java单例模式详解

时间:2015-06-13 20:18:49      阅读:315      评论:0      收藏:0      [点我收藏+]

1、懒汉模式:

      特点:lazy loading很明显,也就是在需要的时候才加载,也就是我们常说的延迟加载。

(1)线程不安全:

public class Singleton {  
    private static Singleton instance;  
  
    public static Singleton getInstance() {  
    if (instance == null) {  
        instance = new Singleton();  
    }  
    return instance;  
    }  
}
(2)线程安全:

  1. public class Singleton {  
  2.     private static Singleton instance;  
  3.   
  4.     public static synchronized Singleton getInstance() {  
  5.     if (instance == null) {  
  6.         instance = new Singleton();  
  7.     }  
  8.     return instance;  
  9.     }  
  10. }


java单例模式详解

原文:http://blog.csdn.net/ccrzzu/article/details/46484207

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!