哈喽!大家好,我是【Bug 终结者】 ,【CSDNJava优质创作者】,阿里云技术博主,51CTO人气博主,INfoQ写作专家
一位上进心十足,拥有极强学习力的【Java领域博主】
【Bug 终结者】博客的领域是【面向后端技术】的学习,未来会持续更新更多的【后端技术】以及【学习心得】。 偶尔会分享些前端基础知识,会更新实战项目,面向企业级开发应用!
如果有对【后端技术】、【前端领域】感兴趣的【小可爱】,欢迎关注【Bug 终结者】
❤️❤️❤️ 感谢各位大可爱小可爱! ❤️❤️❤️
单例模式(Singleton Pattern)是 Java 中最简单的设计模式之一。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
这种模式涉及到一个单一的类,该类负责创建自己的对象,同时确保只有单个对象被创建。这个类提供了一种访问其唯一的对象的方式,可以直接访问,不需要实例化该类的对象。
注意
单例模式经常用在需要一个实例的程序中,例如
JDK中也有单例模式的身影,例
优点
缺点
package com.wanshi.single;
//饿汉式单例
public class Hungry {
//会造成资源浪费,占用CPU
private byte[] data1 = new byte[1024*1024];
private byte[] data2 = new byte[1024*1024];
private byte[] data3 = new byte[1024*1024];
private byte[] data4 = new byte[1024*1024];
private Hungry() {
System.out.println("Hungry init...");
}
private static Hungry hungry = new Hungry();
public static Hungry getInstance() {
return hungry;
}
}
class Test {
public static void main(String[] args) {
Hungry hungry = Hungry.getInstance();
Hungry hungry2 = Hungry.getInstance();
System.out.println(hungry);
System.out.println(hungry2);
}
}
package com.wanshi.single;
import java.lang.reflect.Constructor;
// enum 是一个class类
public enum EnumSingle {
INSTANCE;
public static EnumSingle getInstance() {
return INSTANCE;
}
}
class Test {
public static void main(String[] args) throws Exception{
EnumSingle instance1 = EnumSingle.INSTANCE;
Constructor<EnumSingle> declaredConstructor = EnumSingle.class.getDeclaredConstructor(String.class, int.class);
declaredConstructor.setAccessible(true);
EnumSingle instance2 = declaredConstructor.newInstance();
System.out.println(instance1);
System.out.println(instance2);
}
}
枚举类最后反编译源码
jad工具反编译
jad -sjava EnumSingle.class
// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3)
// Source File Name: EnumSingle.java
package com.wanshi.single;
public final class EnumSingle extends Enum
{
public static EnumSingle[] values()
{
return (EnumSingle[])$VALUES.clone();
}
public static EnumSingle valueOf(String name)
{
return (EnumSingle)Enum.valueOf(com/wanshi/single/EnumSingle, name);
}
private EnumSingle(String s, int i)
{
super(s, i);
}
public static EnumSingle getInstance()
{
return INSTANCE;
}
public static final EnumSingle INSTANCE;
private static final EnumSingle $VALUES[];
static
{
INSTANCE = new EnumSingle("INSTANCE", 0);
$VALUES = (new EnumSingle[] {
INSTANCE
});
}
}
package com.wanshi.single;
public class Lazy {
private static Lazy lazy;
public static Lazy getInterface() {
synchronized (Lazy.class) {
if (lazy == null) {
lazy = new Lazy();
}
}
return lazy;
}
public static void main(String[] args) {
Lazy lazy = Lazy.getInterface();
Lazy lazy2 = Lazy.getInterface();
System.out.println(lazy);
System.out.println(lazy2);
}
}
package com.wanshi.single;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
public class LazyMan {
private static boolean flag = false;
private LazyMan() {
synchronized (this) {
if (!flag) {
flag = true;
} else {
throw new RuntimeException("不要试图通过反射破坏对象");
}
}
}
private volatile static LazyMan lazyMan;
//双重检查锁,懒汉式(DCL懒汉式)
public static LazyMan getInstance() {
if (lazyMan == null) {
synchronized (LazyMan.class) {
if (lazyMan == null) {
//不是原子性操作,1.分配内存空间,2.执行构造方法,3.把对象指向这个空间 指令重排可能会发生 加上volatile关闭指令重排
lazyMan = new LazyMan();
}
}
}
return lazyMan;
}
public static void main(String[] args) throws Exception {
// LazyMan lazyMan1 = LazyMan.getInstance();
Field flag = LazyMan.class.getDeclaredField("flag");
flag.setAccessible(true);
Constructor<LazyMan> declaredConstructor = LazyMan.class.getDeclaredConstructor(null);
declaredConstructor.setAccessible(true);
LazyMan lazyMan1 = declaredConstructor.newInstance();
flag.set(lazyMan1, false);
LazyMan lazyMan2 = declaredConstructor.newInstance();
System.out.println(lazyMan1);
System.out.println(lazyMan2);
}
}
为什么要使用 volatile
关键字呢
不是原子性操作
1.分配内存空间,2.执行构造方法,3.把对象指向这个空间
指令重排可能会发生 加上volatile关闭指令重排
package com.wanshi.single;
public class Holder {
private Holder() {
}
public static class InnerClass {
private static final Holder HOLDER = new Holder();
}
}
案例全部通过测试!
以上就是【Bug 终结者】对手写单例模式及原理剖析的讲解,单例模式共有5种创建方式,分别为饿汉式、DCL懒汉式、双检锁懒汉式、Enum枚举饿汉式,内部类懒汉式,这几种方式要掌握,项目中对于全局唯一的对象将其封装为单例模式,开箱即用,非常方便,以及面试中,会让手写单例模式,可谓是大厂必备!
如果这篇【文章】有帮助到你,希望可以给【Bug 终结者】点个赞,创作不易,如果有对【后端技术】、【前端领域】感兴趣的小可爱,也欢迎关注❤️❤️❤️ 【Bug 终结者】❤️❤️❤️,我将会给你带来巨大的【收获与惊喜】!