单例模式:
-
要点1:当前类只能有一个实例 – 构造器私有化
-
要点2:当前类必须自行创建实例; – 使用当前类的静态变量保存这个唯一的实例;
-
要点3:必须自行向整个系统提供这个实例; – 对外提供获取该实例对象的方式;(1)直接暴露;(2)用静态变量的get方法获取;
常见形式:
-
饿汉式:在类初始化时直接创建对象,不存在线程安全问题;不管是否需要这个对象
1. 直接实例化饿汉式 – 简单直观;见Singleton1
2. 枚举式 – 最简洁;见Singleton2
3. 静态代码块饿汉式 – 适合复杂实例化;见Singleton3 -
懒汉式:延迟创建对象
1. 线程不安全 – 使用于多线程;见Singleton4
2. 线程安全 – 使用于多线程;见Singleton5
3. 静态内部类形式 – 使用于多线程;见Singleton6
/**
* 单例模式 -- 饿汉式 -- 直接实例化饿汉式 -- 简单直观;
*/
public class Singleton1 {
private Singleton1() {
}
public static final Singleton1 INSTANCE = new Singleton1();
}
/**
* 单例模式 -- 饿汉式 --枚举类;限定对象只有一个,就成了单例;
*/
public enum Singleton2 {
INSTANCE;
}
/**
* 单例模式 -- 饿汉式 -- 3.静态代码块饿汉式 -- 适合复杂实例化
*/
public class Singleton3 {
public static final Singleton3 INSTANCE;
private String info;
static {
try {
Properties properties = new Properties();
properties.load(Singleton3.class.getClassLoader().getResourceAsStream("singleton.properties"));
String info1 = properties.getProperty("info");
INSTANCE = new Singleton3(info1);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private Singleton3(String info) {
this.info = info;
}
/**
* 单例模式 -- 懒汉式:延迟创建对象 -- 线程不安全
*/
public class Singleton4 {
private static Singleton4 instance;
private Singleton4() {
}
public static Singleton4 getInstance() {
if (instance == null) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
instance = new Singleton4();
}
return instance;
}
}
// 验证Singleton4的线程安全问题
public static void main(String[] args) throws ExecutionException, InterruptedException {
Callable<Singleton4> callable = new Callable<Singleton4>() {
@Override
public Singleton4 call() throws Exception {
return Singleton4.getInstance();
}
};
ExecutorService es = Executors.newFixedThreadPool(2);
Future<Singleton4> f1 = es.submit(callable);
Future<Singleton4> f2 = es.submit(callable);
System.out.println(f1.get());
System.out.println(f2.get());
es.shutdown();
}
// Singleton4@1d81eb93
// Singleton4@7291c18f
/**
* 单例模式 -- 懒汉式:延迟创建对象 -- 线程安全 -- 枷锁
*/
public class Singleton5 {
private static Singleton5 instance;
private Singleton5() {
}
public static Singleton5 getInstance() {
// 保证性能
if (instance == null) {
// 保证线程安全
synchronized (Singleton5.class) {
if (instance == null) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
instance = new Singleton5();
}
}
}
return instance;
}
}
验证同上述验证Singleton4
/**
* 单例模式 -- 懒汉式:延迟创建对象 -- 静态内部类形式 -- 使用于多线程
*/
public class Singleton6 {
private static class Inner {
// 在内部类被加载和初始化时,才创建INSTANCE对象,是线程安全的;
// 静态内部类不会自动随着外部类的加载和初始化而初始化,而是单独加载和初始化的
private static final Singleton6 INSTANCE = new Singleton6();
}
private Singleton6() {
}
public static Singleton6 getInstance() {
return Inner.INSTANCE;
}
}
小结:
饿汉式,枚举形式最简单;
懒汉式,静态内部类形式最简单;