1、饿汉式
package com.example.singleton;
/**
* 饿汉式
*/
public class EhanSingleton {
private static final EhanSingleton instance = new EhanSingleton();
private EhanSingleton() {
System.out.println("EhanSingleton: 我被执行了!");
}
public static EhanSingleton getInstance() {
return instance;
}
public static void main(String[] args) {
for (int i = 0; i < 20; i++) {
new Thread(() -> {
System.out.println(EhanSingleton.getInstance());
}).start();
}
}
}
2、懒汉式
package com.example.singleton;
/**
* 懒汉式
*/
public class LazyHanSingleton {
private static LazyHanSingleton instance = null;
private LazyHanSingleton() {
System.out.println("LazyHanSingleton: 我被执行了!");
}
public static LazyHanSingleton getInstance() {
if (instance == null) {
instance = new LazyHanSingleton();
}
return instance;
}
public static void main(String[] args) {
for (int i = 0; i < 20; i++) {
new Thread(() -> {
System.out.println(LazyHanSingleton.getInstance());
}).start();
}
}
}
3、懒汉式 + synchronized
package com.example.singleton;
/**
* 懒汉式 + synchronized
*/
public class LazyHanSynSingleton {
private static LazyHanSynSingleton instance = null;
private LazyHanSynSingleton() {
System.out.println("LazyHanSynSingleton: 我被执行了!");
}
public synchronized static LazyHanSynSingleton getInstance() {
if (instance == null) {
instance = new LazyHanSynSingleton();
}
return instance;
}
public static void main(String[] args) {
for (int i = 0; i < 20; i++) {
new Thread(() -> {
System.out.println(LazyHanSynSingleton.getInstance());
}).start();
}
}
}
4、DCL模式
package com.example.singleton;
/**
* DCL模式
*/
public class DoubleCheckLockingSingleton {
private static DoubleCheckLockingSingleton instance = null;
private DoubleCheckLockingSingleton() {
System.out.println("DoubleCheckLockingSingleton: 我被执行了!");
}
public static DoubleCheckLockingSingleton getInstance() {
if (instance == null) {
synchronized (DoubleCheckLockingSingleton.class) {
if (instance == null) {
instance = new DoubleCheckLockingSingleton();
}
}
}
return instance;
}
public static void main(String[] args) {
for (int i = 0; i < 20; i++) {
new Thread(() -> {
System.out.println(DoubleCheckLockingSingleton.getInstance());
}).start();
}
}
}
5、DCL + volatile
package com.example.singleton;
/**
* DCL模式 + Volatile
*/
public class DoubleCheckLockingVolatileSingleton {
private volatile static DoubleCheckLockingVolatileSingleton instance = null;
private DoubleCheckLockingVolatileSingleton() {
System.out.println("DoubleCheckLockingVolatileSingleton: 我被执行了!");
// xxx = new xxx();
// yyy = new yyy();
// instance = new DoubleCheckLockingVolatileSingleton();
// 假如发生指令重排,instance = new DoubleCheckLockingVolatileSingleton();跑到第一行时,
// instance则会被创建,此时 xxx 和 yyy 还未被创建,若其他线程此刻获取该单例对象时,instance 已经被创建完成,
// 若其他线程此刻调用了 xxx 或 yyy 对象,则会出现空指针异常,所以加上 volatile 关键字,防止指令重排。
}
public static DoubleCheckLockingVolatileSingleton getInstance() {
if (instance == null) {
synchronized (DoubleCheckLockingVolatileSingleton.class) {
if (instance == null) {
instance = new DoubleCheckLockingVolatileSingleton();
}
}
}
return instance;
}
public static void main(String[] args) {
for (int i = 0; i < 20; i++) {
new Thread(() -> {
System.out.println(DoubleCheckLockingVolatileSingleton.getInstance());
}).start();
}
}
}
6、holder 静态内部类式
package com.example.singleton;
/**
* holder 静态内部类式
*/
public class HolderSingletonDemo {
private HolderSingletonDemo() {
}
private static class HolderSingleton {
private static HolderSingleton instance = new HolderSingleton();
private HolderSingleton() {
System.out.println("HolderSingleton: 我被执行了!");
}
}
public static HolderSingleton getInstance() {
return HolderSingleton.instance;
}
public static void main(String[] args) {
for (int i = 0; i < 20; i++) {
new Thread(() -> {
System.out.println(HolderSingletonDemo.getInstance());
}).start();
}
}
}
7、枚举式
package com.example.singleton;
/**
* 枚举式
*/
public enum EnumSingleton {
INSTANCE;
public EnumSingleton getInstance() {
return INSTANCE;
}
}
8、枚举式 + 懒加载
package com.example.singleton;
/**
* 延时加载枚举模式
*/
public class EnunLazySingleton {
private EnunLazySingleton() {
}
// 延迟加载
private enum EnumHolder {
INSTANCE;
private EnunLazySingleton instance;
EnumHolder() {
instance = new EnunLazySingleton();
}
private EnunLazySingleton getInstance() {
return instance;
}
}
public static EnunLazySingleton getInstance() {
return EnumHolder.INSTANCE.getInstance();
}
}