Question 1. Efficiency Issues: If you do not need to use the Singleton class object, how to ensure that the Singleton class object will not be created;
Question 2. Thread Safety : How to guarantee that the acquired object is a singleton.
1. The general list of examples is:
class singleton{ privatestaticnull;
Private Singleton () { } publicstatic Singleton getinstance () { if null) { new Singleton (); } return instance;} }
Note: This write only guarantees a single instance in a single thread .
2. You can resolve thread safety issues synchronously, but with relatively low efficiency
class singleton{ privatestaticnull;
Private Singleton () { } publicstaticsynchronized Singleton getinstance () { ifnull) { new Singleton (); } return instance;} }
3. In fact, there are other wording:
classsingleton{Private StaticSingleton instance =NewSingleton (); PrivateSingleton () {} Public StaticSingleton getinstance () {returninstance; } Public Static inti = 3; Public Static voidTest () {System.out.println ("Test invoked."); }}
Note: This solves the problem of synchronization, but when the static test () method is called or the static variable i is called, even if you do not need to use the Singleton object, he is already loaded into memory .
4. The recommended wording is as follows: using an internal class approach, the JVM guarantees that its member variables are initialized and thread-safe when used with internal classes.
class singleton{ private Singleton () { } privatestatic class Singletonholder { publicstaticnew Singleton (); } Public Static Singleton getinstance () { return singletonholder.instance; } }
Add:
Java class loading process: When a class is initialized.
A Java file from being loaded to being unloaded in this life process has a total of four stages to go through:
load - on (Validate + prepare + Parse) initialization (pre-use preparation), use , uninstall
When to initialize a class, the JVM has strict rules (four cases):
1. When encountering the New,getstatic,putstatic,invokestatic 4 bytecode directive, the join class is not initialized yet, it is initialized immediately. In fact, there are 3 situations: when you instantiate a class with new, read or set the static fields of the class (not including the final modified static fields, because they were already plugged into the constant pool when the class was compiled), and when the static method was executed .
2. When using the Java.lang.reflect.* method to make a reflection call to a class, if the class has not yet been initialized, do so immediately.
3. Initialize a class, if his father has not been initialized, first to initialize his father.
4. When the JVM starts, the user needs to specify a main class to execute (the class that contains the static void main (string[] args), the JVM initializes the class first.
The above 4 preprocessing is called an active reference to a class, and the rest of the other cases, called passive references, do not trigger the initialization of the class.
Problems with Java Singleton mode attention