单例模式
- 保证一个类只有一个对象
定义
让类自身负责保存它的唯一实例,这个类可以保证没有其他实例可以被创建,并且它可以提供一个访问该实例的方法。
- 确保某个类只有一个实例,而且自行实例化并向整个系统提供这个实例。
- 1、单例类只能有一个实例。
2、单例类必须自己创建自己的唯一实例。
3、单例类必须给所有其他对象提供这一实例。
单例模式保证了全局对象的唯一性,比如系统启动读取配置文件就需要单例保证配置的一致性。 - 一方面在获取单例的时候,要保证不能产生多个实例对象
- 另一方面,在使用单例对象的时候,要注意单例对象内的实例变量是会被多线程共享的,推荐使用无状态的对象,不会因为多个线程的交替调度而破坏自身状态导致线程安全问题,比如我们常用的VO,DTO等(局部变量是在用户栈中的,而且用户栈本身就是线程私有的内存区域,所以不存在线程安全问题)。
饿汉模式
* 饿汉式
* 类初始化时直接创建实例对象,不管你是否需要这个对象都会创建
*
* 1.构造器私有化
* 2.自行创建实例,并用静态变量保存
* 3.向外提供这个实例public
* 4.为了强调这是一个单例,我们使用final修饰(final修饰的一般为常量,所以我们大写它)
1.直接实例化饿汉式
简洁直观
public class Singleton {
private static Singleton instance = new Singleton();
private Singleton() {
}
public static Singleton getInstance(){
return instance;
}
}
/**
* 饿汉式
* 直接创建实例对象,不管你是否需要这个对象都会创建
*
* 1.构造器私有化
* 2.自行创建实例,并用静态变量保存
* 3.向外提供这个实例public
* 4.为了强调这是一个单例,我们使用final修饰(final修饰的一般为常量,所以我们大写它)
*/
public class Singleton1 {
public static final Singleton1 INSTANCE = new Singleton1();
private Singleton1(){
}
}
public class TestSingleton1 {
public static void main(String[] args) {
Singleton1 s = Singleton1.INSTANCE;
System.out.println(s);
}
}
//com.my.single.Singleton1@1b6d3586
//因为没有重写tostring方法,所以显示的是类名和HashCode码
2.枚举类型
最简洁
/**
* 枚举类型,表示该类型对象是有限的几个
* 我们可以限定为一个,就成为单例了
*/
public enum Singleton2 {
INSTANCE
}
public class TestSingleton2 {
//枚举类不能实例化
public static void main(String[] args) {
Singleton2 s = Singleton2.INSTANCE;
System.out.println(s);
}
}
//INSTANCE(打印出来的是对象的名字)
3.静态代码块饿汉式
适合复杂实例化
public class Singleton3 {
public static final Singleton3 INSTANCE;
private Singleton3(){
}
static {
INSTANCE = new Singleton3();
}
}
举例:
读取配置文件single.properties中的值
info = zzz
import java.io.IOException;
import java.util.Properties;
public class Singleton3 {
public static final Singleton3 INSTANCE;
private String info;//属性需要去初始化
private Singleton3(String info){
this.info = info;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
//值需要从文件中去读取
//文件在src下面才能在类加载器中去加载
static {
try {
Properties properties = new Properties();
properties.load(Singleton3.class.getClassLoader().getResourceAsStream("single.properties"));
INSTANCE = new Singleton3(properties.getProperty("info"));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public String toString() {
return "Singleton3{" +
"info='" + info + '\'' +
'}';
}
}
public class TestSingleton3 {
public static void main(String[] args) {
Singleton3 s = Singleton3.INSTANCE;
System.out.println(s);
}
}
//Singleton3{info='zzz'}
懒汉模式
/**
* 懒汉式
* 延迟创建这个实例对象
*
* 1.构造器私有化
* 2.用一个静态变量保存这个唯一的实例
* 3.提供一个静态方法,获取这个实例对象
*/
1.线程不安全
/**
* @Author: zhang yu zhu
* @Date: 2022/3/25 20:03
* 懒汉式
* 延迟创建这个实例对象
*
* 1.构造器私有化
* 2.用一个静态变量保存这个唯一的实例
* 3.提供一个静态方法,获取这个实例对象
*/
public class Singleton4 {
//此时就不能用public修饰了,
//因为那样可以通过类名直接调用,又可能那个时候是空的
private static Singleton4 instance;
private Singleton4(){
}
//创建过一次之后就不用再创建了
public static Singleton4 getInstance(){
if(instance == null){
instance = new Singleton4();
}
return instance;
}
}
public class TestSingleton4 {
public static void main(String[] args) {
//单线程情况下没问题,get到的都是同一个实例
Singleton4 s1 = Singleton4.getInstance();
Singleton4 s2 = Singleton4.getInstance();
System.out.println(s1 == s2);
System.out.println(s1);
System.out.println(s2);
}
}
//true
//com.my.single.Singleton4@1b6d3586
//com.my.single.Singleton4@1b6d3586
下面看一下多线程的效果:
为了让效果更明显,添加一个sleep休眠:
public class Singleton4 {
//此时就不能用public修饰了,
//因为那样可以通过类名直接调用,又可能那个时候是空的
private 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;
}
}
import java.util.concurrent.*;
public class TestSingleton4 {
public static void main(String[] args) 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 s1 = f1.get();
Singleton4 s2 = f2.get();
System.out.println(s1 == s2);
System.out.println(s1);
System.out.println(s2);
es.shutdown();
}
}
此时就会出现false:
false
com.my.single.Singleton4@7f31245a
com.my.single.Singleton4@6d6f6e28
分析:
我们假设有一个线程A判断完Instance为null,准备要创建实例时,CPU时间片到期,不得不进行切换;线程B判断时候Instance同样为null,它便进行了创建实例的操作,而此时线程A继续执行之前的操作,它也会创建一个实例,我们便不能保证只有一个实例被创建了,所以是线程不安全的!!
2.线程安全
使用同步解决
直接加一个对象锁synchronized(obj)
public class Singleton5 {
private static Singleton5 instance;
private Singleton5(){
}
public static Singleton5 getInstance(){
synchronized (Singleton5.class){
if(instance == null){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
instance = new Singleton5();
}
return instance;
}
}
}
优化一下性能:
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(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
instance = new Singleton5();
}
}
}
return instance;
}
}
3.静态内部类形式
线程安全的
/**
* 在内部类被加载和初始化的时候才会创建INSTANCE实例对象
* 静态内部类不会自动随着外部类的加载和初始化而初始化,它是要单独去加载和初始化的
* 因为是在内部类加载和初始化时候创建的,因此是线程安全的
*/
public class Singleton6 {
private Singleton6(){
}
private static class Inner{
private static final Singleton6 INSTANCE = new Singleton6();
}
public static Singleton6 getInstance(){
return Inner.INSTANCE;
}
}
今日推歌
—《人间城》
缠绕喧嚣中藏匿的
入夜来临时恐惧着
这城漂浮着千百个你和我
无声 无色
你想要在这城遇见优秀的人更华丽的衣着
那就要吞下苦混着泥沙泡沫奔波
有谁会给机会与你成天逍遥快活
倒不如寥寥一瞬 个人努力生活