?? //单例模式之懒汉式 class Singleton{ private static Singleton instance = null; private Singleton(){ } public static Singleton getSingleton(){ if(instance == null){ //同步代码块,保证安全性 synchronized(Singleton.class){ if(instance == null){ instance = new Singleton(); } } } return instance; } } public class TestSingleton { /** * @Title: main * @Description: * @param: * @return void * @user: wangzg * @Date:2014-10-27 * @throws */ public static void main(String[] args) { // TODO Auto-generated method stub Singleton singleton1 = Singleton.getSingleton(); Singleton singleton2 = Singleton.getSingleton(); System.out.println(singleton1 == singleton2); } }
原文地址:http://blog.csdn.net/sd_tz_wzg/article/details/40507059