Five wording singleton

/ ** 
 * lazy man 
 * such an approach can work well in multi-thread, and it looks good with lazy loading, but, unfortunately, inefficient, synchronization is not required under 99% of cases 
 * @ Administrator author 
 * 
 * / 
public class the Singleton { 
	Private the Singleton static instance; 
	
	Private the Singleton Singleton; 
	
	Private the Singleton () {} 
	
	public static getSingleton the synchronized the Singleton (the Singleton instance) { 
		IF (instance == null) { 
			return new new the Singleton (); 
		} 
		return instance; 
	} 
}

  

/ ** 
 * starving mode 
 * This method is based on the synchronization mechanism avoids classloader multithreading, however, instance when instantiated and class loading, although the lead contained in the class 
 there are many reasons *, in most cases the single mode getInstance methods are invoked, but it is not determined in other ways (or other static) cause the class loader 
 * this apparently did not reach instance initialization lazy loading effect 
 * @author Administrator 
 * 
 * / 
public class the Singleton { 

	Private the Singleton static instance; 
	
	Private the Singleton () {} 
	
	public static the Singleton the getInstance (the Singleton instance) { 
		return instance; 
	} 
}

  

/**
 * 静态内部类方法
 * @author Administrator
 *
 */
public class Singleton {

	public static class SingletonHolder{
		private static Singleton singleton = new Singleton();
	}
	
	private Singleton() {}
	
	private static Singleton getInstatnce() {
		return SingletonHolder.singleton;
	}
}

  

/ ** 
 * enumeration method 
 * implemented with three features Singleton enum: free serialization, thread-safe, to ensure that a single embodiment 
 * the sequence consisting of: and each of an enumerated type enumeration in the JVM are unique pieces Some methods of the compiler is disabled to ensure that no damage when called singleton. 
 * Security Thread: enum class can not be inherited, is the final type; similar to a hungry man, object static when the initial creation of the first call is thread-safe. 
 * Single ensure Example: enum only a private constructor; Enumerations are similar and use of internal static class loading method, further, if an instance of the enumeration, not worked lazy loading 
 * @author Administrator 
 * 
 * / 
public enum {singleton 
	INSTANCE; 
	public void whateverMethod () { 
		
	} 
}

  

/**
 * 双重校验锁
 * @author Administrator
 *
 */
public class Singleton {

	private volatile static Singleton singleton;
	
	private Singleton() {}
	
	private static Singleton getSingleton() {
		if(singleton == null) {
			synchronized(Singleton.class) {
				if(singleton == null) {
					singleton = new Singleton();
				}
			}
		}
		
		return singleton;
	}
}

  

 

Reference article:

Singleton five kinds of writing     https://www.cnblogs.com/smile361/p/6506874.html

Enumeration enum and java applications: enumeration single-mode embodiment     https://www.cnblogs.com/cielosun/p/6596475.html

Guess you like

Origin www.cnblogs.com/waiwai4701/p/11257414.html