152_枚举模式实现线程安全的单例

public class SingletonEnumTest {
	
	private SingletonEnumTest() {
		
	}
	
	public static SingletonEnumTest getInstance() {
		return Singleton.INSTANCE.getInstance();
	}

	private enum Singleton{
		INSTANCE;
		
		private SingletonEnumTest singleton;
		
		//JVM保证这个方法只会被调用一次
		Singleton(){
			singleton = new SingletonEnumTest();
		}
		
		public SingletonEnumTest getInstance() {
			return singleton;
		}
	}
	
}

 

你可能感兴趣的:(Java,枚举模式单例)