In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "there are several ways to write the singleton pattern in java", the content is simple and clear, and I hope it can help you solve your doubts. Let the editor lead you to study and learn this article "there are several ways to write singleton patterns in java".
1. Hungry Han style
The hungry Han style is a classic way to realize the singleton pattern design. The implementation code is as follows:
The key to the implementation of the hungry Chinese style is that instance is initialized directly as a class variable. If we take the initiative to use the SingleToEhangshi class, then the instance instance will be created directly, including the instance variables will also be initialized.
Instance, as a class variable, is collected into the () method during class initialization, which ensures 100% synchronization, that is, instance cannot be initialized twice in the case of multithreading. However, because instance is not used for a long time after it is loaded by ClassLoader, it means that the heap memory opened by instance instances will stay for a long time.
Generally speaking, if there are few member variables in a class and do not take up much memory resources, it is okay to implement the singleton pattern in a hungry way, but it can not be loaded lazily.
2. Lazy style
The so-called lazy style is to create a class instance when you use it, that is, I create it when I use it, so that you can avoid creating the class in advance and taking up memory space prematurely. The implementation code is as follows:
/ / final does not allow inherited public final class SingleTonLhangshi {/ / instance variable private byte [] data = new byte [1024]; / / defines an instance, but does not directly initialize the private static SingleTonLhangshi instance = null; / / privatization constructor, and does not allow external NEW private SingleTonLhangshi () {} public static SingleTonLhangshi getInstance () {if (null = = instance) {instance = new SingleTonLhangshi () } return instance;}}
Lazy Chinese writing in a multi-threaded environment, there will be multiple threads at the same time to see the null==instance, resulting in instance will be instantiated many times, which can not guarantee the uniqueness of the singleton.
3. Lazy + synchronization method
The lazy singleton implementation can guarantee the lazy loading of the instance, but it can not guarantee the uniqueness of the instance. In the multithreaded environment, because instance is a shared data, when multiple threads access and use, we need to ensure the synchronization of the data, so if we need to ensure the uniqueness of lazy instances, we can achieve it through synchronization. The code is as follows:
/ final does not allow inherited public final class SingleTonLhangshiSync {/ / instance variable private byte [] data = new byte [1024]; / / define instances, but do not directly initialize private static SingleTonLhangshiSync instance = null
The lazy + data synchronization method not only satisfies lazy loading, but also ensures the uniqueness of instance instances. However, the exclusivity of the synchronized keyword can cause the getInstance () method to be accessed by only one thread at a time, resulting in poor performance.
4 、 Double-Check
Double-Check is a clever way to design, it provides an efficient data synchronization strategy, that is, first initialize with a lock, and then allow multiple threads to call the getInstance () method at the same time to obtain an instance of the class. The code is as follows:
/ / initialize} public static SingletonDoubleCheck getInstance () {/ / when instance is null, enter the synchronization code block, and this judgment avoids the need to enter the synchronization code block every time Can improve efficiency if (null = = instance) {/ / only one thread can obtain the monitor synchronized (SingletonDoubleCheck.class) associated with SingletonDoubleCheck.class {/ / determine that if instance is null, create if (null = = instance) {instance = new SingletonDoubleCheck () } return instance;}}
When two threads find that the null==instance is established, only one thread is qualified to enter the synchronization code block and complete the initialization of the instance. The subsequent thread finds that the null==instance is not established and does not need to do anything, and the future access to the getInstance will no longer need data synchronization.
This approach not only satisfies lazy loading, but also ensures the uniqueness of instance instances, and also provides a more efficient data synchronization strategy that allows multiple threads to access getInstance at the same time. However, in the case of multithreading, this may cause a null pointer exception, because if there is a case of initializing other resources in the construction method of the above code, because of the instruction rearrangement at JVM runtime, the order of these resources is not constrained by the context when instantiating, then it is very likely that instance is the first to be instantiated, while con and socket are not fully instantiated. An instance that has not been instantiated will throw a null pointer exception when calling its method.
5 、 Volatile+Double-Check
To solve the null pointer problem caused by Double-Check instruction rearrangement, the volatile keyword can be used to prevent such reordering from happening. Therefore, the code only needs to be modified slightly to meet the singleton, lazy loading and instance efficiency in multithreading. The code is as follows:
/ / final does not allow inherited public final class SingletonDoubleCheck {/ / instance variable private byte [] data = new byte [1024]; / / defines an instance, but does not directly initialize the private static volatile SingletonDoubleCheck instance = null; Connection con; Socket socket; / / privatization constructor, and does not allow external NEW private SingletonDoubleCheck () {this.con = con;// initialization this.socket = socket / / initialize} public static SingletonDoubleCheck getInstance () {/ / when instance is null, enter the synchronization code block, and this judgment avoids the need to enter the synchronization code block every time Can improve efficiency if (null = = instance) {/ / only one thread can obtain the monitor synchronized (SingletonDoubleCheck.class) associated with SingletonDoubleCheck.class {/ / determine that if instance is null, create if (null = = instance) {instance = new SingletonDoubleCheck () } return instance;}}
6. Holder mode
The Holder method makes full use of the characteristics of class loading. The code is as follows:
/ / the inherited public final class SingletonHolder {/ / instance variable private byte [] data = new byte [1024] is not allowed; private SingletonHolder () {} / / holds an instance of the singleton class in the static inner class, and can be directly initialized private static class Holder {private static SingletonHolder instance = new SingletonHolder () } / / call the getInstance method, which actually obtains the instance static property public static SingletonHolder getInstance () {return Holder.instance;}} of Holder
There is no static member of instance in the singleton class, but it is placed in the static inner class Holder, so the singleton class does not create an instance of SingletonHolder during initialization. The inner class Holder defines the static variable of SingletonHolder and instantiates it directly. Only when Holder is actively referenced will an instance of SingletonHolder be created.
The creation process of SingletonHolder instance is collected into the () method during the compilation of Java program, which is also a synchronous method, which can ensure the visibility of memory, the sequence and atomicity of JVM instructions. Holder singleton pattern design is one of the best designs, and it is also a widely used design at present.
7. Enumeration mode
Enumeration is also widely used in many open source frameworks, enumerated types are not allowed to be inherited, are also thread-safe, and can only be instantiated once, but enumerated types can not be loaded lazily. With enumerated types, the code to implement the singleton pattern is as follows:
Public class SingletonEnum {/ / instance variable private byte [] data = new byte [1024]; private SingletonEnum () {} / / uses enumerations to act as Holder private enum EnumHolder {INSTANCE; private SingletonEnum instance; EnumHolder () {this.instance = new SingletonEnum ();} private SingletonEnum getInstance () {return instance }} public static SingletonEnum getInstance () {return EnumHolder.INSTANCE.getInstance ();}} these are all the contents of the article "there are several ways to write singleton patterns in java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.