?? //单例模式之懒汉式 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); } }
时间: 2024-10-14 16:10:18