单例的7种实现
TYPE01-- 饿汉式(静态变量)
public class SingletonTest01 {
public static void main ( String[ ] args) {
for ( int i = 0 ; i < 10 ; i++ ) {
new Thread ( ( ) - > {
Singleton instance = Singleton. getInstance ( ) ;
System. out. println ( Thread. currentThread ( ) . getName ( ) + " instance.hashCode:" + instance. hashCode ( ) ) ;
} ) . start ( ) ;
}
}
}
class Singleton {
private Singleton ( ) {
}
private final static Singleton instance = new Singleton ( ) ;
public static Singleton getInstance ( ) {
return instance;
}
}
TYPE02 – 饿汉式(静态代码块)
public class SingletonTest02 {
public static void main ( String[ ] args) {
for ( int i = 0 ; i < 10 ; i++ ) {
new Thread ( ( ) - > {
Singleton instance = Singleton. getInstance ( ) ;
System. out. println ( Thread. currentThread ( ) . getName ( ) + " instance.hashCode:" + instance. hashCode ( ) ) ;
} ) . start ( ) ;
}
}
}
class Singleton {
private Singleton ( ) {
}
private static Singleton instance;
static {
instance = new Singleton ( ) ;
}
public static Singleton getInstance ( ) {
return instance;
}
}
TYPE03 – 懒汉式(线程不安全)
public class SingletonTest03 {
public static void main ( String[ ] args) {
for ( int i = 0 ; i < 10 ; i++ ) {
new Thread ( ( ) - > {
Singleton instance = Singleton. getInstance ( ) ;
System. out. println ( Thread. currentThread ( ) . getName ( ) + " instance.hashCode:" + instance. hashCode ( ) ) ;
} ) . start ( ) ;
}
}
}
class Singleton {
private static Singleton instance;
private Singleton ( ) {
}
public static Singleton getInstance ( ) {
if ( instance == null) {
instance = new Singleton ( ) ;
}
return instance;
}
}
TYPE04 – 懒汉式( 线程安全)
public class SingletonTest04 {
public static void main ( String[ ] args) {
for ( int i = 0 ; i < 10 ; i++ ) {
new Thread ( ( ) - > {
Singleton instance = Singleton. getInstance ( ) ;
System. out. println ( Thread. currentThread ( ) . getName ( ) + " instance.hashCode:" + instance. hashCode ( ) ) ;
} ) . start ( ) ;
}
}
}
class Singleton {
private static Singleton instance;
private Singleton ( ) {
}
public synchronized static Singleton getInstance ( ) {
if ( instance == null) {
instance = new Singleton ( ) ;
}
return instance;
}
}
TYPE05 – 双重检查
public class SingletonTest05 {
public static void main ( String[ ] args) {
for ( int i = 0 ; i < 10 ; i++ ) {
new Thread ( ( ) - > {
Singleton instance = Singleton. getInstance ( ) ;
System. out. println ( Thread. currentThread ( ) . getName ( ) + " instance.hashCode:" + instance. hashCode ( ) ) ;
} ) . start ( ) ;
}
}
}
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;
}
}
TYPE06 – 静态内部类
public class SingletonTest06 {
public static void main ( String[ ] args) {
for ( int i = 0 ; i < 10 ; i++ ) {
new Thread ( ( ) - > {
Singleton instance = Singleton. getInstance ( ) ;
System. out. println ( Thread. currentThread ( ) . getName ( ) + " instance.hashCode:" + instance. hashCode ( ) ) ;
} ) . start ( ) ;
}
}
}
class Singleton {
private Singleton ( ) {
}
private static class SingletonInstance {
private static final Singleton INSTANCE = new Singleton ( ) ;
}
public static Singleton getInstance ( ) {
return SingletonInstance. INSTANCE;
}
}
TYPE07 – 枚举
public class SingletonTest7 {
public static void main ( String[ ] args) {
for ( int i = 0 ; i < 10 ; i++ ) {
new Thread ( ( ) - > {
Singleton instance = Singleton. SINGLETON;
System. out. println ( Thread. currentThread ( ) . getName ( ) + " instance.hashCode:" + instance. hashCode ( ) ) ;
} ) . start ( ) ;
}
}
}
enum Singleton {
SINGLETON;
public void show ( ) {
System. out. println ( "我是通过枚创建单例" ) ;
}
}