饿汉模式:实例在类加载的时候就创建好。直接上代码,两种实现方式。两种实现方式因为都是在类加载的时候就实例化好的,所以是线程安全的。
/**
* 饿汉模式
* 单例实例在类装载时进行创建
* 线程安全
*/
public class SingletonExample2 {
// 私有构造函数
private SingletonExample2() {
}
// 单例对象
private static SingletonExample2 instance = new SingletonExample2();
// 静态的工厂方法
public static SingletonExample2 getInstance() {
return instance;
}
}
/**
* 饿汉模式
* 单例实例在类装载时进行创建
* 线程安全
*/
public class SingletonExample6 {
// 私有构造函数
private SingletonExample6() {
}
// 单例对象
private static SingletonExample6 instance = null;
static {
instance = new SingletonExample6();
}
// 静态的工厂方法
public static SingletonExample6 getInstance() {
return instance;
}
}
懒汉模式是指在第一次获取实例的时候才实例化单例,有可能同时会有多个线程去获取单例,线程的安全性需要考虑。
/**
* 懒汉模式
* 单例实例在第一次使用时进行创建
* 这种方式是线程不安全的
*/
public class SingletonExample1 {
// 私有构造函数
private SingletonExample1() {
}
// 单例对象
private static SingletonExample1 instance = null;
// 静态的工厂方法
//线程不安全,如果线程A和B同时访问这个方法
public static SingletonExample1 getInstance() {
//A判断为true 则继续执行下一步,但是此时单例还没有实例化好,B也进入这个判断,又重新实例化一遍。
if (instance == null) {
instance = new SingletonExample1();
}
return instance;
}
}
/**
* 懒汉模式
* 单例实例在第一次使用时进行创建
* 线程安全的 但是不推荐使用
*/
public class SingletonExample3 {
// 私有构造函数
private SingletonExample3() {
}
// 单例对象
private static SingletonExample3 instance = null;
// 静态的工厂方法
//synchronized修改静态方法,保证每次只能有一个线程访问到这个方法内部
public static synchronized SingletonExample3 getInstance() {
if (instance == null) {
instance = new SingletonExample3();
}
return instance;
}
}
/**
* 懒汉模式 -》 双重同步锁单例模式
* 单例实例在第一次使用时进行创建
* 线程不安全来源于jvm的指令重排
*/
public class SingletonExample4 {
// 私有构造函数
private SingletonExample4() {
}
//实例化对象的正常的顺序
// 1、memory = allocate() 分配对象的内存空间
// 2、ctorInstance() 初始化对象
// 3、instance = memory 设置instance指向刚分配的内存
// JVM和cpu优化,发生了指令重排
// 1、memory = allocate() 分配对象的内存空间
// 3、instance = memory 设置instance指向刚分配的内存
// 2、ctorInstance() 初始化对象
// 单例对象
private static SingletonExample4 instance = null;
// 静态的工厂方法
//如果A执行到3,此时instance != null了,但是实际上instance还没有创建好,
//B执行到if语句,直接返回了没有创建好的instance,线程不安全
public static SingletonExample4 getInstance() {
if (instance == null) { // 双重检测机制 // B
synchronized (SingletonExample4.class) { // 同步锁
if (instance == null) {
instance = new SingletonExample4(); // A - 3
}
}
}
return instance;
}
}
/**
* 懒汉模式 -》 双重同步锁+ volatile 关键字禁止指令重排,线程安全
* 单例实例在第一次使用时进行创建
*
*/
public class SingletonExample5 {
// 私有构造函数
private SingletonExample5() {
}
// 1、memory = allocate() 分配对象的内存空间
// 2、ctorInstance() 初始化对象
// 3、instance = memory 设置instance指向刚分配的内存
// 单例对象 volatile + 双重检测机制 -> 禁止指令重排
private volatile static SingletonExample5 instance = null;
// 静态的工厂方法
public static SingletonExample5 getInstance() {
if (instance == null) { // 双重检测机制 // B
synchronized (SingletonExample5.class) { // 同步锁
if (instance == null) {
instance = new SingletonExample5(); // A - 3
}
}
}
return instance;
}
}
枚举模式
/**
* 枚举模式:最安全 最推荐
*/
public class SingletonExample7 {
// 私有构造函数
private SingletonExample7() {
}
public static SingletonExample7 getInstance() {
return Singleton.INSTANCE.getInstance();
}
private enum Singleton {
INSTANCE;
private SingletonExample7 singleton;
// JVM保证这个方法绝对只调用一次
Singleton() {
singleton = new SingletonExample7();
}
public SingletonExample7 getInstance() {
return singleton;
}
}
}
1278

被折叠的 条评论
为什么被折叠?



