posts - 64,comments - 22,trackbacks - 0
 1
 2 
 3 
 4 /**
 5  * java中实现单例的最好的方法,既保证了延迟加载,又保证了线程安全
 6  * @author wolf
 7  *
 8  */
 9 public class Singleton {
10     /**
11      * 类级的内部类,也就是静态的成员式内部类,该类内部的实例与外部的实例
12      * 没有绑定关系,而且只有被调用到时才会装载,从而实现了延迟加载
13      */
14     
15     private static class SingletonHolder{
16         /**
17          * 静态初始化器,由JVM来保证线程安全
18          */
19         
20         private static Singleton instance = new Singleton();
21     }
22     
23     private Singleton(){
24         
25     }
26     
27     public static Singleton getInstance(){
28         return SingletonHolder.instance;
29     }
30 }
31 
posted on 2011-11-24 15:43 hellxoul 阅读(188) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: