当前位置:网站首页>Singleton and invariant modes of concurrent mode
Singleton and invariant modes of concurrent mode
2022-07-29 03:18:00 【Program cat Dagang】
The singleton pattern
Singleton mode is to ensure that there is only one instance of a class in the system .
In many ways , Static inner class method is recommended , Use the class load once feature to ensure that there is only one instance ( Thread safety ),
At the same time, it has the advantages of lock free and lazy creation .
Example :
public class StaticInnerClassSingleton {
// Privatized construction method
private StaticInnerClassSingleton(){}
// Inner class
static class Holder{
static StaticInnerClassSingleton INSTANCE = new StaticInnerClassSingleton();
}
// Get instance object
public static StaticInnerClassSingleton getInstance(){
return Holder.INSTANCE;
}
public static void main(String[] args) {
for(int i=0;i<10;i++){
new Thread(()->{
System.out.println(StaticInnerClassSingleton.getInstance());
}).start();
}
}
}
Immutable mode
In a concurrent scenario , Invariant objects do not require synchronous operations .( Because the object will not change , Therefore, there will be no inconsistency in the access of multiple threads )
features :
- class Be modified as final
- Properties are private and are final Embellished
JDK All metadata classes in 、 Wrapper classes are implemented using invariant patterns .
java.lang.String
java.lang.Boolean
java.lang.Byte
java.lang.Character
java.lang.Double
java.lang.Float
java.lang.Integer
java.lang.Long
java.lang.Short
Example :
public final class FinalCat {// final Make sure there are no subclasses
private final String name;// final Ensure properties
private final Integer age;
public FinalCat(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public Integer getAge() {
return age;
}
@Override
public String toString() {
return "FinalCat{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public static void main(String[] args) {
FinalCat cat = new FinalCat(" Pikanzi ", 4);
System.out.println(cat);
}
}
边栏推荐
- 微信为之疯狂的Glide使用——之生命周期学习
- My approval function of conference OA project
- Score addition and subtraction of force deduction and brushing questions (one question per day 7/27)
- 国产ERP有没有机会击败SAP ?
- 3D advanced renderer: artlandis studio 2021.2 Chinese version
- C traps and defects Chapter 3 semantic "traps" 3.8 operators &, |, and!
- 年内首个“三连跌” 95号汽油回归“8元时代“
- MYSQL入门与进阶(十四)
- A simple and general method to obtain the size of function stack space
- C traps and defects Chapter 3 semantic "traps" 3.4 avoid "couple method"
猜你喜欢

Design of smoke temperature, humidity and formaldehyde monitoring based on single chip microcomputer

带你来浅聊一下,单商户功能模块汇总

Watermelon book learning Chapter 6 -- SVM

01-SDRAM:初始化模块的代码

「PHP基础知识」输出圆周率的近似值

13_ue4进阶_蒙太奇动画实现一边走一边攻击

【科技1】

Linux下安装MySQL8.0的详细步骤

Inventory of domestic and foreign project collaborative management software: SAAS and customization become a trend

力扣刷题之数组序号计算(每日一题7/28)
随机推荐
How to deploy sentinel cluster of redis
Introduction and advanced MySQL (XIV)
逐步分析类的拆分之案例——五彩斑斓的小球碰撞
Unity 之游戏特效
增量实时灾备笔记
Calculation of array serial number of force deduction questions (daily question 7/28)
Apache文件管理自学笔记——映射文件夹和基于单ip多域名配置apache虚拟机
Shell script summary
Production deployment zabbix5.0 notes
Does domestic ERP have a chance to beat sap?
Digital image processing Chapter 10 - image segmentation
LeetCode 1331 数组序号转换[Map] HERODING的LeetCode之路
Military product development process - transition phase
【机器人学习】机械臂抓手matlab运动学与admas动力学分析
3D advanced renderer: artlandis studio 2021.2 Chinese version
照片比例校正工具:DxO ViewPoint 3 直装版
力扣刷题之分数加减运算(每日一题7/27)
July 28, 2022 Gu Yujia's study notes
2022-07-28 第四小组 修身课 学习笔记(every day)
【C】数组