6种单例模式
什么是单例模式
Singleton:单例设计模式,
某各类在整个系统中只能有一个实例对象可被获取和使用的代码模式
例如:jvm运行环境的Runtime类
代码要点
1、这个类一定只能有一个实例——构造器私有化
2、这个实例必须自行创建——含有一个该类的静态变量来保存这个唯一的实例
3、必须自行向这个系统提供这个实例——对外提供获取该实例对象的方式:直接暴露或用静态变量的get方法获取
分类
饿汉模式: 直接创建对象,不存在线程安全问题
直接实例化(简洁直观)
public class Singleton1 {
public static final Singleton1 INSTANCE=new Singleton1();
private Singleton1(){
}
public static void main(String[] args){
Singleton1 s=Singleton1.INSTANCE;
System.out.println(s);
System.out.println(s);
System.out.println(s);
}
}
运行结果:
枚举式(最简洁)
public enum Singleton2 {
INSTANCE
}
class....
public static void main(String[] args){
Singleton2 s2=Singleton2.INSTANCE;
System.out.println(s2);
System.out.println(s2);
System.out.println(s2);
}
运行结果:
静态代码块饿汉式(适合复杂实例化)
public class Singleton3 {
public static final Singleton3 INSTANCE;
private String info;
static{
Properties properties=new Properties();
try {
properties.load(Singleton3.class.getClassLoader().getResourceAsStream("single.properties"));
} catch (IOException e) {
e.printStackTrace();
}
INSTANCE=new Singleton3(properties.getProperty("info"));
}
private Singleton3(String info){
this.info=info;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
}
class....
public static void main(String[] args){
Singleton3 s3=Singleton3.INSTANCE;
System.out.println(s3);
System.out.println(s3);
System.out.println(s3);
}
运行结果:
懒汉式:延迟创建对象
线程不安全(适用于单线程)
public class Singleton4 {
static Singleton4 instance;
private Singleton4(){
}
public static Singleton4 getInstance(){
if(instance==null){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
instance=new Singleton4();
}
return instance;
}
}
class ...
main ...{
test();
}
//会出现线程安全问题
public static void test() throws ExecutionException, InterruptedException {
//但是会存在线程安全问题,例如
Callable<Singleton4> c=new Callable<Singleton4>() {
@Override
public Singleton4 call() throws Exception {
return Singleton4.getInstance();
}
};
ExecutorService es= Executors.newFixedThreadPool(2);
Future<Singleton4> f1=es.submit(c);
Future<Singleton4> f2=es.submit(c);
Singleton4 s3=f1.get();
Singleton4 s4=f2.get();
System.out.println(s3==s4);
System.out.println(s3);
System.out.println(s4);
}
运行结果:
线程安全(适用于多线程)
public class Singleton5 {
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) {
e.printStackTrace();
}
instance=new Singleton5();
}
}
}
return instance;
}
}
用上面的那个验证方法验证结果为:
静态内部类形式(适用于多线程)
public class Singleton6 {
private Singleton6(){
}
private static class Inner{
private static final Singleton6 INSTANCE=new Singleton6();
}
public static Singleton6 getInstance(){
return Inner.INSTANCE;
}
}
总结:
- 如果是饿汉式,枚举类型最简单
- 如果是懒汉式,静态内部类形式最简单,
- 因为静态内部类不会自动随着外部类的加载和初始化而初始化,它是要单独去加载和初始的
- 因为是内部类加载和初始化时创建的,既可以延迟加载又保证线程安全
参考:尚硅谷视频