Once to a company interview, interview asked the Java design pattern in a single case mode, let me write out with a pen, perhaps their own technology has not formed a system, the idea is not very clear, the results in a piece of paper written on the mess, the smear is not, unsightly. Often think of the scene at that time, it is really difficult to forget, but this experience is also good for me, let me have more understanding of the single case mode.
First, consider how you can guarantee that a Java class has only one of the multithreaded environments throughout the application context:
1th: Cannot write the public constructor, avoids this class to carry on the new object operation;
2nd: To ensure that the single example object properties and methods are class-level, in line with the Java Bean specifications, attributes are not exposed, only to provide public access methods;
3rd: To ensure that the creation of a single Case object is thread-safe;
Next look at the code:
1, lazy type: When used to create, and can not guarantee thread safety, need to add lock mechanism
public class Singleton {
//private class variable private
static Singleton Singleton;
Private constructor Private
Singleton () {
}
//public class method publicly
static/*synchronized*/Singleton getinstance () {
Synchronized (singleton.class) {
//when used to create
if (Singleton = null) {
Singleton = new Singleton ();
} return
Singleton;
//}
}
}
2, a hungry man: first created, when you want to use directly, this way should be thread-safe, class loading (ClassLoader) phase on the instantiation of the singleton
public class Singleton {
//private class variable private
static final Singleton Singleton = new Singleton ();
Private constructor Private
Singleton () {
}
//Public-class method publicly
static Singleton getinstance () {
//To be used directly
Return singleton
}
}
3. Double check mechanism: If the single Instance object is empty, get the class lock first, then judge whether the single instance object is empty, and create the object on the null.
public class Singleton {
//private class variable, volatile variable disables instruction reordering
private static volatile Singleton Singleton;
Private constructor Private
Singleton () {
}
//public class method publicly
static Singleton getinstance () {
if ( Singleton = = null) {
synchronized (singleton.class) {
if (singleton = = null) {
singleton = new Singleton ();
}
}
}
Return singleton
}
}
4. Static internal class: Define a static inner class within the class, initialize it when calling inner class, execute once, ensure thread safety
public class Singleton {
//private constructor Private
Singleton () {
}
//publicly
static Singleton method GetInstance () {return
innersingleton.instance;
}
Static internal class static
final class Innersingleton {
private static final Singleton instance = new Singleton ();
}
}
5, Static code block: In the class loading phase is instantiated, static code block is only executed once.
public class Singleton {
private static Singleton Singleton;
static{
singleton = new Singleton ();
}
Private constructor Private
Singleton () {
}
//public class method publicly
static Singleton getinstance () {
return Singleton
}
}