当前位置:网站首页>如何破坏单例?我说了好几种方式,面试官:没想到你真会
如何破坏单例?我说了好几种方式,面试官:没想到你真会
2022-07-29 15:26:00 【InfoQ】
反射破坏单例
import java.io.Serializable;/** * 使用双重校验锁方式实现单例 */public class Singleton implements Serializable{ private volatile static Singleton singleton; private Singleton (){} public static Singleton getSingleton() { if (singleton == null) { synchronized (Singleton.class) { if (singleton == null) { singleton = new Singleton(); } } } return singleton; }}
import java.lang.reflect.Constructor;public class SingletonTest { public static void main(String[] args) { Singleton singleton = Singleton.getSingleton(); try { Class<Singleton> singleClass = (Class<Singleton>)Class.forName("com.dev.interview.Singleton"); Constructor<Singleton> constructor = singleClass.getDeclaredConstructor(null); constructor.setAccessible(true); Singleton singletonByReflect = constructor.newInstance(); System.out.println("singleton : " + singleton); System.out.println("singletonByReflect : " + singletonByReflect); System.out.println("singleton == singletonByReflect : " + (singleton == singletonByReflect)); } catch (Exception e) { e.printStackTrace(); } }}
singleton : [email protected] : [email protected] == singletonByReflect : false
private Singleton() { if (singleton != null) { throw new RuntimeException("Singleton constructor is called... "); }}
Caused by: java.lang.RuntimeException: Singleton constructor is called...
序列化破坏单例
public class SingletonTest { public static void main(String[] args) { Singleton singleton = Singleton.getSingleton(); //Write Obj to file ObjectOutputStream oos = null; try { oos = new ObjectOutputStream(new FileOutputStream("tempFile")); oos.writeObject(singleton); //Read Obj from file File file = new File("tempFile"); ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file)); Singleton singletonBySerialize = (Singleton)ois.readObject(); //判断是否是同一个对象 System.out.println("singleton : " + singleton); System.out.println("singletonBySerialize : " + singletonBySerialize); System.out.println("singleton == singletonBySerialize : " + (singleton == singletonBySerialize)); } catch (Exception e) { e.printStackTrace(); } }}
singleton : [email protected] : [email protected] == singletonBySerialize : false
private Object readResolve() { return getSingleton();}
边栏推荐
猜你喜欢
Flink SQL Hudi 实战
Altair SimSolid complex assembly meshless rapid structure simulation online seminars
Shell script programming - operation
[yolov7 series two] positive and negative sample allocation strategy
@RequestMapping注解最详细解析
【GoLang】同步锁
【Go语言刷题篇】Go完结篇函数、结构体、接口、错误入门学习
Win11 最新补丁更新破坏了任务栏中的“开始”菜单
Linux下载安装mysql5.7版本教程最全详解
Jmeter实现多用户测试
随机推荐
MySQL笔记下
数据中台建设(四):企业构建数据中台评估
金九银十必备 快来看看你还缺啥
cmake(14):利用set_property命令设置全局属性
AI全流程开发难题破解之钥
Jenkins持续集成与自动化部署系统安装配置
货比三家 tb1.3
腾讯云数据库负责人林晓斌借1亿炒股:已爆仓破产
你真的了解Redis的持久化机制吗?
一文参透分布式存储系统Ceph的架构设计、集群搭建(手把手)
Detailed evaluation of Renesas RZ/G2L processor
手摸手实现Canal如何接入MySQL实现数据写操作监听
JUL 学习
奇怪,为什么ArrayList初始化容量大小为10?
3分钟带你了解微信小程序开发
进入中国27年,又一美妆巨头要离场
每日优鲜倒下,下一个是谁?
[MySQL] 运算符
浮点数内存存储问题
c语言之位域