黑马程序员技术交流社区

标题: 单例模式的饿汉式和懒汉式 [打印本页]

作者: lc030405    时间: 2015-1-29 19:04
标题: 单例模式的饿汉式和懒汉式
单例模式的饿汉式和懒汉式。
   1>饿汉式
        public class Single{
            private static Single s = new Single();
            private Single(){};
            public static Single getInstance(){
                 return s;
            }
        }

   2>懒汉式
        public class Single{
            private static Single = null;
            private Single(){};

/*
            public static synchornized Single getInstance(){
                if(s==null)
                    s= new Single();
                return s;
            }
*/
            public static Single getInstance(){
                 if(s==null){
                      synchornized(Single.class){
                          if(s==null)
                               s = new Single();
                       }
                 }
                 return s;
            }

        }


作者: 流星划过的黎明    时间: 2015-1-29 21:52
呃。。。




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2