单例模式详细总结
一、饿汉式两种方式
1、静态变量直接new出来
package com. gykalc. jdk8. singleton. type01 ;
public class SingletonTest01 {
public static void main ( String [ ] args) {
Singleton singleton = Singleton . getInstance ( ) ;
Singleton singleton1 = Singleton . getInstance ( ) ;
System . out. println ( singleton == singleton1) ;
System . out. println ( "singleton HashCode: " + singleton. hashCode ( ) ) ;
System . out. println ( "singleton1 HashCode: " + singleton1. hashCode ( ) ) ;
}
}
class Singleton {
private static final Singleton instance = new Singleton ( ) ;
private Singleton ( ) { }
public static Singleton getInstance ( ) {
return instance;
}
}
2、静态代码块new出来
package com. gykalc. jdk8. singleton. type02 ;
public class SingletonTest01 {
public static void main ( String [ ] args) {
Singleton singleton = Singleton . getInstance ( ) ;
Singleton singleton1 = Singleton . getInstance ( ) ;
System . out. println ( singleton == singleton1) ;
System . out. println ( "singleton HashCode: " + singleton. hashCode ( ) ) ;
System . out. println ( "singleton1 HashCode: " + singleton1. hashCode ( ) ) ;
}
}
class Singleton {
private static final Singleton instance;
static {
instance = new Singleton ( ) ;
}
private Singleton ( ) { }
public static Singleton getInstance ( ) {
return instance;
}
}
二、懒汉式两种方式
1、单线程模式
package com. gykalc. jdk8. singleton. type03 ;
public class SingletonTest01 {
public static void main ( String [ ] args) {
Singleton singleton = Singleton . getInstance ( ) ;
Singleton singleton1 = Singleton . getInstance ( ) ;
System . out. println ( singleton == singleton1) ;
System . out. println ( "singleton HashCode: " + singleton. hashCode ( ) ) ;
System . out. println ( "singleton1 HashCode: " + singleton1. hashCode ( ) ) ;
}
}
class Singleton {
private static Singleton instance;
private Singleton ( ) { }
public static Singleton getInstance ( ) {
if ( instance == null ) {
instance = new Singleton ( ) ;
}
return instance;
}
}
2、支持多线程
package com. gykalc. jdk8. singleton. type04 ;
public class SingletonTest01 {
public static void main ( String [ ] args) {
Singleton singleton = Singleton . getInstance ( ) ;
Singleton singleton1 = Singleton . getInstance ( ) ;
System . out. println ( singleton == singleton1) ;
System . out. println ( "singleton HashCode: " + singleton. hashCode ( ) ) ;
System . out. println ( "singleton1 HashCode: " + singleton1. hashCode ( ) ) ;
}
}
class Singleton {
private static Singleton instance;
private Singleton ( ) { }
public static synchronized Singleton getInstance ( ) {
if ( instance == null ) {
instance = new Singleton ( ) ;
}
return instance;
}
}
双重检查
package com. gykalc. jdk8. singleton. type05 ;
public class SingletonTest01 {
public static void main ( String [ ] args) {
Singleton singleton = Singleton . getInstance ( ) ;
Singleton singleton1 = Singleton . getInstance ( ) ;
System . out. println ( singleton == singleton1) ;
System . out. println ( "singleton HashCode: " + singleton. hashCode ( ) ) ;
System . out. println ( "singleton1 HashCode: " + singleton1. hashCode ( ) ) ;
}
}
class Singleton {
private static volatile Singleton instance;
private Singleton ( ) { }
public static Singleton getInstance ( ) {
if ( instance == null ) {
synchronized ( Singleton . class ) {
if ( instance == null ) {
instance = new Singleton ( ) ;
}
}
}
return instance;
}
}
静态内部类方式
package com. gykalc. jdk8. singleton. type06 ;
public class SingletonTest01 {
public static void main ( String [ ] args) {
Singleton singleton = Singleton . getInstance ( ) ;
Singleton singleton1 = Singleton . getInstance ( ) ;
System . out. println ( singleton == singleton1) ;
System . out. println ( "singleton HashCode: " + singleton. hashCode ( ) ) ;
System . out. println ( "singleton1 HashCode: " + singleton1. hashCode ( ) ) ;
}
}
class Singleton {
private Singleton ( ) { }
public static Singleton getInstance ( ) {
return SingletonFactory . getInstance ( ) ;
}
private static class SingletonFactory {
private static final Singleton instance = new Singleton ( ) ;
public static Singleton getInstance ( ) {
return instance;
}
}
}
枚举方式
package com. gykalc. jdk8. singleton ;
public class SingletonTest01 {
public static void main ( String [ ] args) {
Singleton singleton = Singleton . INSTANCE ;
Singleton singleton1 = Singleton . INSTANCE ;
System . out. println ( singleton == singleton1) ;
System . out. println ( "singleton HashCode: " + singleton. hashCode ( ) ) ;
System . out. println ( "singleton1 HashCode: " + singleton1. hashCode ( ) ) ;
}
}
enum Singleton {
INSTANCE ;
}