当前位置:网站首页>Detailed single case mode
Detailed single case mode
2022-06-30 18:52:00 【Z boo boo】
List of articles
- The singleton pattern
- 1. Hungry han singleton
- 2. An example of lazy style
- 3. Static inner class gets the singleton
- 4. Reflection breaking singleton mode
- Normal acquisition
- Broken ring one : A single instance is obtained in the normal way , A single instance is obtained by reflection
- Broken ring II , Create objects without using the references provided in the class , Every time you create an object, you use the reflection method
- Broken ring three , By breaking the fields in the class , Destroy the traffic light strategy
- 5. The way to enumerate , Prevent single cases from being destroyed
The singleton pattern
1. Hungry han singleton
characteristic : First, instantiate the object
Existing problems : It may cause a waste of resources and space
/** * @Description: The hungrier singleton * @Author: Boo, boo, boo, boo ~~~ * @Date:2022/6/29 */
public class Hungry {
/** * Suppose this class opens up a large array when it is created * * The problem with this starving singleton mode is that it may cause a waste of resource space * For example, this class will create many large arrays , So the singleton pattern is to instantiate the object * If this object is not used later, a lot of space will be wasted * Therefore, according to the problems of the hungry type, the lazy type singleton mode is introduced */
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];
/** * A private constructor */
private Hungry() {
}
/** * Hungry han singleton , No matter three, seven, twenty-one , Load the object first */
private final static Hungry HUNGRY = new Hungry();
public static Hungry getInstance() {
return HUNGRY;
}
}
2. An example of lazy style
//--------------------- Base version singleton mode , There are thread safety issues ----------------------
public class LazyMan {
/** * Private structure */
private LazyMan() {
}
/** * Don't instantiate the object first */
private static LazyMan lazyMan;
/** * Check whether the following objects are empty , When it is empty, it is created * @return */
public static LazyMan getInstance() {
if (lazyMan == null) {
lazyMan = new LazyMan();
}
return lazyMan;
}
}
//----------------------- Dual detection lock mode version -----------------------------
public class LazyMan {
/** * Private structure */
private LazyMan() {
}
/** * The double detection lock object is given as volatile To embellish , Prevent command rearrangement , because : * lazyMan = new LazyMan(); This new The operation of objects is not atomic at the bottom * 1. Allocate memory space * 2. Execute construction , Initialize object * 3. Point this object to this space * Suppose now A The thread executes the code that creates the instance , Instruction rearrangement occurs at the bottom The order of three steps to create an object is 1 3 2 * It means threads A The object points to the space before it is initialized , Suppose there is a thread B So far * Threads B In judging the first lazyMan==null When it comes to false, Because the thread A Has pointed to memory space * So at this point, the thread B This object is returned directly , But actually threads A This object has not been initialized yet , So the object returned is nihilistic * So in order to rearrange instructions when creating objects , Add one more volatile Keyword modification */
private volatile static LazyMan lazyMan;
/** * Check whether the following objects are empty , When it is empty, it is created * @return */
public static LazyMan getInstance() {
// This is the first one lazyMan == null The function of is to improve efficiency , Without this layer , Get the lock every time , Getting a lock is actually a very efficient operation
if (lazyMan == null) {
synchronized (LazyMan.class) {
if (lazyMan == null) {
lazyMan = new LazyMan();
}
}
}
return lazyMan;
}
}
3. Static inner class gets the singleton
public class Holder {
/** * Constructor private */
private Holder() {
}
public static Holder getInstance() {
return InnerClass.HOLDER;
}
/** * Create instances of static inner classes */
public static class InnerClass{
private static final Holder HOLDER = new Holder();
}
}
4. Reflection breaking singleton mode
Normal acquisition
public static void main(String[] args) {
LazyMan instance1 = LazyMan.getInstance();
LazyMan instance2 = LazyMan.getInstance();
System.out.println(instance1.hashCode());
System.out.println(instance2.hashCode());
}
Broken ring one : A single instance is obtained in the normal way , A single instance is obtained by reflection
public static void main(String[] args) throws Exception {
LazyMan instance1 = LazyMan.getInstance();
// Get constructor of singleton class
Constructor<LazyMan> declaredConstructor = LazyMan.class.getDeclaredConstructor(null);
// Ignore private constructors
declaredConstructor.setAccessible(true);
// Get instances through reflection
LazyMan instance2 = declaredConstructor.newInstance();
System.out.println(instance1.hashCode());
System.out.println(instance2.hashCode());
}
- resolvent , Improve private constructors , The way of triple detection lock , Let's judge from the construction method of the singleton class ,instance1 Is it empty , If it is not empty, an exception is thrown
/** * Private structure */
private LazyMan() {
synchronized (LazyMan.class) {
if (lazyMan != null) {
// The constructor is also called if it is not empty , It means that it is destroyed by reflection , Throw an exception to prevent
throw new RuntimeException(" Do not attempt to use the reflection breaking singleton mode !!!");
}
}
}
Broken ring II , Create objects without using the references provided in the class , Every time you create an object, you use the reflection method
public static void main(String[] args) throws Exception {
// Get constructor of singleton class
Constructor<LazyMan> declaredConstructor = LazyMan.class.getDeclaredConstructor(null);
// Ignore private constructors
declaredConstructor.setAccessible(true);
// Get instances through reflection
LazyMan instance1 = declaredConstructor.newInstance();
LazyMan instance2 = declaredConstructor.newInstance();
System.out.println(instance1.hashCode());
System.out.println(instance2.hashCode());
}
- The solution to this situation , It can be solved by making a mark
/** * Traffic light strategy , Prevent single case cup damage */
private static boolean target = false;
/** * Private structure */
private LazyMan() {
if (!target) {
target = true;
} else {
throw new RuntimeException(" Don't try to break the loop by reflection ");
}
}
Broken ring three , By breaking the fields in the class , Destroy the traffic light strategy
public static void main(String[] args) throws Exception {
// Get constructor of singleton class
Constructor<LazyMan> declaredConstructor = LazyMan.class.getDeclaredConstructor(null);
// Ignore private constructors
declaredConstructor.setAccessible(true);
// Get... In the singleton class target Field
Field target = LazyMan.class.getDeclaredField("target");
target.setAccessible(true);
// Get instances through reflection
LazyMan instance1 = declaredConstructor.newInstance();
// After execution , We change the value of the field to false
target.set(instance1,false);
LazyMan instance2 = declaredConstructor.newInstance();
System.out.println(instance1.hashCode());
System.out.println(instance2.hashCode());
}
The law is strong !!!
5. The way to enumerate , Prevent single cases from being destroyed
/** * @Description: Enumeration prevents the singleton from being destroyed * enum It's also a class * @Author: Boo, boo, boo, boo ~~~ * @Date:2022/6/30 */
public enum EnumSingle {
INSTANCE;
public EnumSingle getInstance() {
return INSTANCE;
}
}
class Test {
public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
EnumSingle instance1 = EnumSingle.INSTANCE;
Constructor<EnumSingle> declaredConstructor = EnumSingle.class.getDeclaredConstructor(null);
declaredConstructor.setAccessible(true);
EnumSingle instance2 = declaredConstructor.newInstance();
System.out.println(instance1);
System.out.println(instance2);
}
}
- The final decompiled enumeration type is a parameterized constructor
- Reflection violation enumeration throws an exception
边栏推荐
- Adhering to the concept of 'home in China', 2022 BMW children's traffic safety training camp was launched
- 【云驻共创】Huawei iConnect使能物联终端一触即联
- Electronic components bidding and purchasing Mall: optimize traditional purchasing business and speed up enterprise digital upgrading
- 「经验」我对用户增长的理解『新用户篇』
- 剑指 Offer 17. 打印从1到最大的n位数
- 剑指 Offer 16. 数值的整数次方
- TiDB Dashboard里面可以写sql执行吗
- Hcip (Huawei Senior Network Security Engineer) (Experiment 8) (MPLS basic experiment)
- Openlayers roller shutter map
- Distributed transaction
猜你喜欢
Geoffrey Hinton:我的五十年深度学习生涯与研究心法
程序员女友给我做了一个疲劳驾驶检测
Helping the ultimate experience, best practice of volcano engine edge computing
depends工具查看exe和dll依赖关系
Four tips tell you how to use SMS to promote business sales?
助力极致体验,火山引擎边缘计算最佳实践
挑选智能音箱时,首选“智能”还是“音质”?这篇文章给你答案
Adhering to the concept of 'home in China', 2022 BMW children's traffic safety training camp was launched
Vulnerability recurrence ----37. Apache unomi Remote Code Execution Vulnerability (cve-2020-13942)
电子元器件招标采购商城:优化传统采购业务,提速企业数字化升级
随机推荐
countdownlatch 和 completableFuture 和 CyclicBarrier
OneFlow源码解析:算子签名的自动推断
Rust 文件系统处理之文件读写 - Rust 实践指南
C language structure
助力极致体验,火山引擎边缘计算最佳实践
【合集- 行业解决方案】如何搭建高性能的数据加速与数据编排平台
Rhai - Rust 的嵌入式脚本引擎
What if icloud photos cannot be uploaded or synchronized?
MySQL找不到mysql.sock文件的临时解
Coding officially entered Tencent conference application market!
冰河老师的书
Openlayers 卷帘地图
Vs Code treeview TreeView
分布式事务
How to do a good job in software system demand research? Seven weapons make it easy for you to do it
医院在线问诊小程序源码 互联网医院源码 智慧医院源码
Force deduction solution summary 1175- prime number arrangement
hdfs上的数据导入到clickhouse用什么方式最快呢?spark通过jdbc导入,还是hdfs
剑指 Offer 16. 数值的整数次方
Geoffrey Hinton: my 50 years of in-depth study and Research on mental skills