Singleton design pattern eight ways --5) lazy formula (thread-safe, synchronized block) 6) 7 double check) internal static type 8) Enumeration

Lazy formula ( thread-safe, synchronized block ) Application Examples

Explain the advantages and disadvantages :

1) In this manner, it was intended to improve the implementation of the fourth, the front synchronization method because low efficiency, to produce the synchronization code blocks instantiated

2) However, this synchronization does not play the role of thread synchronization . With the first 3 consistent implementation kinds of situations encountered, if a thread enters the if (singleton == null) judgment statement block, not enough time to execute down, another thread also passed the judge sentence, this time will produce multiple instances

3) Conclusion: In the actual development, can not be used in this way



Double-checking

SingletonTest06.java

package com.atguigu.singleton.type6;


public class SingletonTest06 {

	public static void main(String[] args) {
		System.out.println("双重检查");
		Singleton instance = Singleton.getInstance();
		Singleton instance2 = Singleton.getInstance();
		System.out.println(instance == instance2); // true
		System.out.println("instance.hashCode=" + instance.hashCode());
		System.out.println("instance2.hashCode=" + instance2.hashCode());
		
	}

}

// 懒汉式(线程安全,同步方法)
class Singleton {
	private static volatile Singleton instance;
	
	private Singleton() {}
	
	//提供一个静态的公有方法,加入双重检查代码,解决线程安全问题, 同时解决懒加载问题
	//同时保证了效率, 推荐使用
	
	public static synchronized Singleton getInstance() {
		if(instance == null) {
			synchronized (Singleton.class) {
				if(instance == null) {
					instance = new Singleton();
				}
			}
			
		}
		return instance;
	}
}

Explain the advantages and disadvantages :

1) Double the Check-concept is multi-threaded development often use to , as shown in the code, we conducted two if (singleton == null) examination, so as to ensure the security thread.

2) Thus, instantiation code used only once, when accessed again later, it is judged if (singleton == null),

    Examples of direct return of the object, but also to avoid the repeated synchronization method.

3) thread-safe; lazy loading; high efficiency

4) Conclusion: In the actual development, this is recommended singleton design pattern



Static inner classes

SingletonTest07.java

package com.atguigu.singleton.type7;


public class SingletonTest07 {

	public static void main(String[] args) {
		System.out.println("使用静态内部类完成单例模式");
		Singleton instance = Singleton.getInstance();
		Singleton instance2 = Singleton.getInstance();
		System.out.println(instance == instance2); // true
		System.out.println("instance.hashCode=" + instance.hashCode());
		System.out.println("instance2.hashCode=" + instance2.hashCode());
		
	}

}

// 静态内部类完成, 推荐使用
class Singleton {
	//构造器私有化
	private Singleton() {}
	
	//写一个静态内部类,该类中有一个静态属性 Singleton
	private static class SingletonInstance {
		private static final Singleton INSTANCE = new Singleton(); 
	}
	
	//提供一个静态的公有方法,直接返回SingletonInstance.INSTANCE
	
	public static synchronized Singleton getInstance() {
		
		return SingletonInstance.INSTANCE;
	}
}

Explain the advantages and disadvantages :

1) This method uses a mechanism to ensure class loading only one thread instance initialization.

2) internal static type manner in Singleton when the class is loaded and instantiated not immediately , but when needed instantiated call getInstance method will load SingletonInstance class, thereby completing Singleton instantiated.

3) static properties of the class is first loaded only when the class is initialized , so here, the JVM to ensure the security thread, when the class is initialized, the other thread is inaccessible.

4) advantage: avoiding thread-safe , use a static inner classes Features implement lazy loading, high efficiency

5) Conclusion: Recommended.



enumerate

SingletonTest08.java

package com.atguigu.singleton.type8;

public class SingletonTest08 {
	public static void main(String[] args) {
		Singleton instance = Singleton.INSTANCE;
		Singleton instance2 = Singleton.INSTANCE;
		System.out.println(instance == instance2);
		
		System.out.println(instance.hashCode());
		System.out.println(instance2.hashCode());
		
		instance.sayOK();
	}
}

//使用枚举,可以实现单例, 推荐
enum Singleton {
	INSTANCE; //属性
	public void sayOK() {
		System.out.println("ok~");
	}
}

Explain the advantages and disadvantages :

1) This means JDK1.5 enumeration added to achieve single-mode embodiment. Not only to avoid the multi-thread synchronization problems , but also to prevent deserialization re-create a new object.

2) This approach is Effective Java author Josh Bloch advocated way

3) Conclusion: recommended

Published 434 original articles · won praise 105 · views 70000 +

Guess you like

Origin blog.csdn.net/qq_39368007/article/details/104932825