- 饿汉式
- class Single{
- private static final Single s=new Single();
- private Single(){}
- public static Single getInstance(){
- return s;
- }
- }
- 延迟加载懒汉式
- class Single{
- private static final Single s=null;
- private Single(){}
- punlic static Single getInstance(){
- if(s==null){
- synchronized(Single.class){
- if(s==null)
- s= new Single();
- }
- }
- return s;
- }
- }
复制代码 |
|