标签:执行 on() 单例模式 hashcode its struct 初始化 加载类 tor
for example: SessionFactory in Hibernate
public class SingletonTest01 {
public static void main(String[] args){
Singleton singleton = Singleton.getInstance();
Singleton singleton2 = Singleton.getInstance();
System.out.println(singleton == singleton2); // true
}
}
class Singleton{
private Singleton(){ //private the constructor
}
private static final Singleton instance = new Singleton();
public static Singleton getInstance(){
return instance;
}
}
装载类的时候,这个类就有了, 避免了线程同步的问题
但同时加载过早可能会造成浪费
class Singleton{
private Singleton(){} //private the constructor
private static Singleton instance;
static{
instance = new Singleton(); // ********
}
public static Singleton getInstance(){
return instance;
}
}
与静态变量法优缺点相同。
public class SingletonTest03 {
public static void main(String[] args){
SingletonLazy singleton = SingletonLazy.getInstance();
SingletonLazy singleton2 = SingletonLazy.getInstance();
System.out.println(singleton == singleton2); // true
System.out.println(singleton.hashCode());
System.out.println(singleton2.hashCode());
}
}
class SingletonLazy{
private static SingletonLazy instance;
private SingletonLazy(){};
// create instance only when called
public static SingletonLazy getInstance(){
if(instance == null){
instance = new SingletonLazy();
}
return instance;
}
}
这个肯定不适合多线程,因为 if(instance == null) 这句 没有锁。
class SingletonLazy{
private static SingletonLazy instance;
private SingletonLazy(){};
// create instance only when called
public static synchronized SingletonLazy getInstance(){
if(instance == null){
instance = new SingletonLazy();
}
return instance;
}
}
但这种方式效率太低,而且getInstance()方法实际上只需执行一次。 NOT RECOMMENDED
class SingletonVolatile{
private static volatile SingletonVolatile singleton;
private SingletonVolatile(){};
public static SingletonVolatile getInstance(){
if(singleton == null){
synchronized(SingletonVolatile.class){
if(singleton == null){ // make sure the "null" condition is in the synchronized block
singleton = new SingletonVolatile()
}
}
}
return singleton;
}
}
class SingletonInnerClass{
private SingletonInnerClass(){};
private static class SingletonInstance{
private static final SingletonInnerClass INSTANCE = new SingletonInnerClass();
} // final可以在底层进行优化
public static SingletonInnerClass getInstance(){
return SingletonInstance.INSTANCE;
}
}
public class SingletonTest04{
public static void main(String[] args){
SingletonEnum singleton1 = SingletonEnum.INSTANCE;
SingletonEnum singleton2 = SingletonEnum.INSTANCE;
System.out.println(singleton1 == singleton2); //true
}
enum SingletonEnum{
INSTANCE;
public void method(){
// here is your code
}
}
}
标签:执行 on() 单例模式 hashcode its struct 初始化 加载类 tor
原文地址:https://www.cnblogs.com/nedrain/p/13196051.html