The use of Java singleton pattern

Keywords: Java Database network

Copyright notice: This is the original article of the blogger. It can't be reproduced without the permission of the blogger. https://blog.csdn.net/u010046908/article/details/50750731

1. Definition of singleton mode

Ensure that there is only one instance in a class, and that it instantiates itself and provides one instance to the entire system.

Singleton in singleton package is a lazy single case, singleton double check case, singleton 2 hungry single case

2. Use scenario of single mode:

Ensure that a class has and only has one instance. Avoid creating multiple objects that consume too much resources, or objects of a certain type should only have and already have one.
For example, creating an object consumes too much resources. To access IO, database and network resources, consider using a single example

3 key points of single mode:

(1) privatization of construction method, generally private
(2) return singleton object through a static method.
(3) ensure that there is only one instance class object, especially in the case of multithreading.

(4) ensure that the singleton class object does not rebuild when deserializing.

4. Code implementation

4.1 example of lazy style

package singleton;
/**
 * Lazy single row mode
 * @author Administrator
 * @Advantage: a single instance can only be initialized when it is used, which saves resources to a certain extent.
 * @Disadvantages: the first load needs to be initialized in time. The response is a little slow. The biggest problem is that getInstance is called every time
 * It's all synchronization, which causes unnecessary overhead. It's generally not recommended.
 */
public class Singleton {

	private static Singleton instance;
	
	private Singleton(){}
	//Synchronization method
	public static synchronized Singleton getInstance(){
	
		if(instance==null){
			instance = new Singleton();
		}
		return instance;
		
	}
	
	public static void main(String[] args) {
		System.out.println();
	}
	
}

4.2 DCL mode
package singleton;

/**
 * 
 * @author lidong
 * @Advantages: it can not only initialize single instance when needed, but also ensure thread safety. And the singleton object is initialized, calling getInstance without synchronizing locks.
 * @Delay loading mode
 */
public class Singleton1 {

	private static Singleton1 instance = null;

	private Singleton1() {
	}

	public static Singleton1 getInstance() {

		if (instance == null) {//Avoid unnecessary synchronization
			synchronized (Singleton1.class) {
				if (instance == null) {//Create instance when null
					instance = new Singleton1();
				}
			}
		}
		return instance;
	}
}

3. Starved Han style

package singleton;
/**
 * A single case of starving Han style
 * @author lidong
 * @Preload mode
 */
public class Singleton2 {

	
	private static Singleton2 instance = new Singleton2();

	private Singleton2() {
	}
	
	public static  Singleton2 getInstance(){
		return instance;
		
	}
	
	
}

Test class
package singleton;

public class Test {
	
	public static void main(String[] args) {
		//Hungry man
		Singleton2 s1 = Singleton2.getInstance();
		Singleton2 s2 = Singleton2.getInstance();
		System.out.println(s1.equals(s2));
		
		//Slacker type
		Singleton s3 = Singleton.getInstance();
		Singleton s4 = Singleton.getInstance();
		System.out.println(s3.equals(s4));
		
		//Double shackle mode
		Singleton1 s5 = Singleton1.getInstance();
		Singleton1 s6 = Singleton1.getInstance();
		System.out.println(s5.equals(s6));
		System.out.println(s1);
		System.out.println(s2);
		System.out.println(s3);
		System.out.println(s4);
		System.out.println(s5);
		System.out.println(s6);
	}
	

}

Result:
true
true
true
singleton.Singleton2@12b6651
singleton.Singleton2@12b6651
singleton.Singleton@4a5ab2
singleton.Singleton@4a5ab2
singleton.Singleton1@1888759
singleton.Singleton1@1888759

Download address

Posted by anto91 on Sun, 08 Dec 2019 07:07:58 -0800