Java Singleton mode and java mode

Source: Internet
Author: User

Java Singleton mode and java mode

The Singleton mode is used to ensure that only one class instance is created during the class operation. In addition, Singleton mode provides a globally unique access point for this class instance, is the getInstance method.

For the singleton mode, No matter what implementation method is used, it only cares about the creation of class instances and does not care about specific business functions.

Solution 1: lazy

Lazy class instances are created in the getInstance method. Lazy is a typical time-to-space change, that is, every time an instance is acquired, it is judged to see whether an instance needs to be created, wasting the time for judgment. Of course, if no one is using it all the time, it will not create an instance, thus saving the memory space.

Package Singleton mode;/*** ** @ author Administrator **/public class Singleton1 {private static Singleton1 uniqueInstance = null; private Singleton1 () {} public static synchronized Singleton1 getInstance () {if (uniqueInstance = null) {uniqueInstance = new Singleton1 ();} return uniqueInstance ;}}

This lazy method does not apply synchronization, so the thread is not secure. You can add synchornized to ensure thread security:

public static synchronized Singleton getInstance(){}

However, this will reduce the speed of the entire access, and each time you have to judge, you can use the "double check lock" to ensure that the thread is secure, and the performance will not be greatly affected.

"Double Check and lock" means that the synchronization is not required every time the getInstance method is entered, but not first. After the method is entered, check whether the instance exists. If the method does not exist, it enters the following synchronization block, this is the first check. after entering the synchronization block, check again whether the instance exists, but it does not exist. Create an instance in the case of synchronization. This is the second check. In this way, you only need to synchronize one time, thus reducing the time wasted in the case of multiple synchronization operations. The implementation is as follows:

Package Singleton mode;/*** double lock lazy style * @ author Administrator **/public class Singleton3 {private volatile static Singleton3 instance = null; private Singleton3 () {} public static Singleton3 getInstance () {if (instance = null) {synchronized (Singleton3.class) {if (instance = null) {instance = new Singleton3 ();}}} return instance ;}}

Solution 2: Hungry Chinese

The hungry Chinese style is a typical space change time. When a class is loaded, a class instance will be created. No matter whether you use it or not, it will be created first, and then each time you perform electrophoresis, no further judgment is required, saving the running time. The hungry Chinese style is thread-safe.

Package Singleton mode;/*** hunger style ** @ author Administrator **/public class Singleton2 {private static Singleton2 uniqueInstance = new Singleton2 (); private Singleton2 () {} public static Singleton2 getInstance () {return uniqueInstance ;}}

Java provides a better Singleton implementation method:


The above two solutions have certain defects. Is there a way to achieve both delayed loading and thread security?

To achieve thread security, you can use the static initializer method. It can be ensured by JVM, such as the previous Hunger style. However, this will waste some space.

If there is a way to let the class load do not initialize the object, won't the problem be solved? One feasible method is to use an internal class to create an object instance in this internal class. As a result, as long as the internal class of this class is not used, no object instance will be created, in this way, latency loading and thread security are achieved simultaneously.

The implementation is as follows:

Package Singleton mode;/*** use internal static class, thread safety and efficiency * @ author Administrator **/public class Singleton4 {private static class SingletonHolder {private static Singleton4 instance = new Singleton4 ();} private Singleton4 () {} public static Singleton4 getInstance () {return SingletonHolder. instance ;}}

PS: Not long ago, I took part in the spring recruitment of zhenai.com. In the interview with java development engineers, I had a question about the thread-safe and efficient Singleton mode, so I could use the internal class method. Unfortunately, I didn't understand it at the time.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.