单例模式:某个类只能存在一个对象
具体实现
package com.designmode.singleton;
/**
* 懒汉式: 1)线程不安全(并发时可能出现多个单例) 2)构造方法为private只能从getInstance去获取单例
* 3)使用static关键字第一次加载getInstance()需要时间
*
* @author hexiaoli
*
*/
public class Singleton1 {
//初始化顺序父类静态-子类静态-父类构造代码块-父类构造函数-子类构造代码块-子类构造函数
private static Singleton1 INSTANCE ;
public static Singleton1 getInstance() {
if (INSTANCE == null) {
INSTANCE = new Singleton1();
}
return INSTANCE;
}
private Singleton1() {}
}
package com.designmode.singleton;
/**
* 懒汉式+synchronized: 1)线程安全2)内存消耗大3)效率低
*
* @author hexiaoli
*
*/
public class Singleton2 {
private static Singleton2 INSTANCE;
public static synchronized Singleton2 getINSTANCE() {
if (INSTANCE == null) {
INSTANCE = new Singleton2();
}
return INSTANCE;
}
private Singleton2() {
}
}
package com.designmode.singleton;
/**
* 懒汉式+双检验: 1)线程安全2)效率低
*
* @author hexiaoli
*
*/
public class Singleton3 {
private static Singleton3 INSTANCE;
public static Singleton3 getINSTANCE() {
// 第一次检验
if (INSTANCE == null) {
// 第二次检验
synchronized (Singleton3.class) {
if (INSTANCE == null) {
INSTANCE = new Singleton3();
}
}
}
return INSTANCE;
}
}
package com.designmode.singleton;
/**
* 饿汉式(空间换时间): 1)线程安全2)实例在类加载时完成实例化
* 与懒汉式区别是,饱汉每次都要进行判断为空再创建实例,采用时间换空间
*
* @author hexiaoli
*
*/
public class Singleton4 {
//声明称公有也可以
private static Singleton4 INSTANCE= new Singleton4();
public static Singleton4 getINSTANCE() {
return INSTANCE;
}
private Singleton4() {
}
}
package com.designmode.singleton;
/**
* 饿汉式+静态构造块: 1)线程安全2)实例在类加载时完成实例化
*
* @author hexiaoli
*
*/
public class Singleton5 {
private static Singleton5 INSTANCE = null;
static {
INSTANCE = new Singleton5();
}
public static Singleton5 getINSTANCE() {
return INSTANCE;
}
private Singleton5() {
}
}
package com.designmode.singleton;
/**
* 饿汉式+静态内部类: 1)线程安全2)效率高 3)推荐
*
* @author hexiaoli
*
*/
public class Singleton6 {
private static class InnerSingleton6{
private static Singleton6 INSTANCE= new Singleton6();
}
public static Singleton6 getINSTANCE() {
return InnerSingleton6.INSTANCE;
}
private Singleton6() {
}
}
package com.designmode.singleton;
/**
* 枚举式: 1)线程安全2)避免反序列化
*
* @author hexiaoli
*
*/
public enum Singleton7 {
INSTANCE;
public Singleton7 getINSTANCE() {
return INSTANCE;
}
}
package com.designmode.singleton;
/**
* CAS方式:1)忙等待的算法,支持较大并行度2)如果一直执行不成功,对CPU造成很大执行开销
* @author hexiaoli
*
*/
import java.util.concurrent.atomic.AtomicReference;
public class Singleton8 {
// AtomicReference是原子引用模型
private static final AtomicReference<Singleton8> INSTANCE = new AtomicReference<>();
public static Singleton8 getInstance() {
while (true) {
Singleton8 instance = INSTANCE.get();
if (instance != null) {
return instance;
}
instance = new Singleton8();
// compareAndSet两个参数,如果当前值等于参数1则更新为参数2,否则不更新返回false
if (INSTANCE.compareAndSet(null, instance)) {
return instance;
}
}
}
}
package com.designmode.singleton;
/**
* lock机制 1)线程安全
* @author hexiaoli
*
*/
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Singleton9 {
private static Singleton9 INSTANCE;
private static Lock lock = new ReentrantLock();
public static Singleton9 getInstance() {
if (INSTANCE == null) {
lock.lock();
INSTANCE = new Singleton9();
}
lock.unlock();
return INSTANCE;
}
private Singleton9() {
}
}
例子: