标签:syn cto ret turn tor style 懒汉 stat 饿汉
一、单例模式的写法:
public class MyFactory { /** * 饿汉式 */ private static MyFactory instance = new MyFactory(); private MyFactory(){ } public static MyFactory getInstance(){ return instance; } }
public class MyFactory { /** * 懒汉式(要注意处理线程安全问题) */ private static MyFactory instance; public static MyFactory getInstance(){ if(instance == null){ synchronized(MyFactory.class){ if(instance == null){ instance = new MyFactory(); } } } return instance; } }
--------------
标签:syn cto ret turn tor style 懒汉 stat 饿汉
原文地址:http://www.cnblogs.com/tenWood/p/7056976.html