单例模式
饿汉式 (静态常量)
优点:写法简单,在类装载时就完成了实例化,避免了线程同步问题
缺点:在类装载时就完成了实例化,有可能从始至终都用不上这个实例,造成内存浪费
public class Single {
private Single() {}
private final static Single single = new Single();
public static Single getInstance() {
return single;
}
}
public class Main {
public static void main(String[] args) {
System.out.println(Single.getInstance() == Single.getInstance());
}
}
console
true
饿汉式 (静态代码块)
这种方式实际与静态常量方式类似,都是在类加载时实例化。优缺点也与静态常量方式相同
public class Single2 {
static {
single = new Single2();
}
private Single2() {}
private final static Single2 single;
public static Single2 getInstance() {
return single;
}
}
public class Main {
public static void main(String[] args) {
System.out.println(Single.getInstance() == Single.getInstance());
System.out.println(Single2.getInstance() == Single2.getInstance());
}
}
console
true
true
懒汉式
有可能会出现多个线程同时抢到了 getInstance 方法的情况,所以线程不安全,慎用
可以通过添加 synchronized 来添加线程锁使线程同步,但是会造成线程拥堵。导致效率变低
public class Single3 {
private Single3() {}
private static Single3 single = null;
public static Single3 getInstance() {
if (null == single) single = new Single3();
return single;
}
// public static synchronized Single3 getInstance() {
// if (null == single) single = new Single3();
// return single;
// }
}
public class Main {
public static void main(String[] args) {
System.out.println(Single.getInstance() == Single.getInstance());
System.out.println(Single2.getInstance() == Single2.getInstance());
System.out.println(Single3.getInstance() == Single3.getInstance());
}
}
console
true
true
true
懒汉式(二)
通过双重验证,再加上 volatile 关键字对变量的修饰达到懒加载/安全并且速度快的单例结果
public class Single4 {
private Single4() {}
// private static Single4 single1 = null;
/**
* 添加 volatile 关键字
* 1、volatile保证可见性
* 2、volatile不保证原子性
* 3、volatile禁止指令重排
*/
private static volatile Single4 single = null;
/* 反面教材 */
// public static Single4 getInstance() {
// if (null == single1) {
// // 方法没有加锁,无法阻止多线程进入 if 方法
// synchronized (Single4.class) {
// single1 = new Single4();
// }
// }
// return single1;
// }
/**
* 通过双重验证,再加上 volatile 关键字对变量的修饰达到懒加载/安全并且速度快的单例结果
* @return
*/
public static Single4 getInstance() {
if (null == single) {
synchronized (Single4.class) {
if (null == single) {
single = new Single4();
}
}
}
return single;
}
}
console
true
true
true
true
true
静态内部类(推荐使用)
线程安全
通过 jvm 虚拟机装载机制来实现单例模式
- 当外部类在被装载时,内部类不会被装载
- 当外部类使用到内部类时,才会装载到内部类。jvm 在装载类时线程是安全的。并且静态内部类只会被加载一次
public class Single5 {
private Single5() {}
private static class SingleInstance {
private static final Single5 single = new Single5();
}
public static Single5 getInstance() {
return SingleInstance.single;
}
}
public class Main {
public static void main(String[] args) {
System.out.println(Single.getInstance() == Single.getInstance());
System.out.println(Single2.getInstance() == Single2.getInstance());
System.out.println(Single3.getInstance() == Single3.getInstance());
System.out.println(Single4.getInstance() == Single4.getInstance());
System.out.println(Single5.getInstance() == Single5.getInstance());
}
}
console
true
true
true
true
true
枚举 (推荐使用)
通过枚举实现单例还可以防止被反射进行多次实例化。是目前最完美的单例机制。
public enum Single6 {
INSTANCE;
public String method() {
return "Hello World";
}
}
public class Main {
public static void main(String[] args) {
System.out.println(Single.getInstance() == Single.getInstance());
System.out.println(Single2.getInstance() == Single2.getInstance());
System.out.println(Single3.getInstance() == Single3.getInstance());
System.out.println(Single4.getInstance() == Single4.getInstance());
System.out.println(Single5.getInstance() == Single5.getInstance());
System.out.println(Single6.INSTANCE == Single6.INSTANCE);
System.out.println(Single6.INSTANCE.method());
}
}
console
true
true
true
true
true
true
Hello World