当前位置:网站首页>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);
}
}
边栏推荐
- 国产ERP有没有机会击败SAP ?
- 2.nodejs--路径(_dirname,_filname)、url网址、querystring模块、mime模块、各种路径(相对路径)、网页的加载(面试题*)
- 正则表达绕过waf
- C traps and defects Chapter 3 semantic "traps" 3.9 integer overflow
- Three military product baselines (functional baseline, distribution baseline, product baseline) and the documents contained in the baseline
- Verilog: blocking assignment and non blocking assignment
- C陷阱与缺陷 第3章 语义“陷阱” 3.3 作为参数的数组声明
- Shell script summary
- Principle knowledge is useful
- C traps and defects Chapter 2 syntax "traps" 2.6 problems caused by "hanging" else
猜你喜欢

Flask creation process day05-06 creation project

Redis configuration cache expiration listening event trigger

CentOS install mysql8

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

Makefile details

MySQL installation and configuration super detailed tutorial and simple database and table building method

Verilog:阻塞赋值和非阻塞赋值

makefile详解

How to deploy sentinel cluster of redis

Algorithm --- paint the house (kotlin)
随机推荐
MySQL large table joint query optimization, large transaction optimization, avoiding transaction timeout, lock wait timeout and lock table
生产部署zabbix5.0笔记
Redis配置缓存过期监听事件触发
01-sdram: Code of initialization module
Li Shuo, vice president of Baidu: it's a good thing that China's labor costs rise with the support of digital technology
Linux下安装MySQL8.0的详细步骤
The Federal Reserve raised interest rates again, Powell "let go of doves" at 75 basis points, and US stocks reveled
Feedback function of conference OA
正则表达绕过waf
Alibaba Sentinel - workflow and principle analysis
13_ UE4 advanced_ Montage animation realizes attack while walking
【C】数组
C traps and defects Chapter 3 semantic "traps" 3.4 avoid "couple method"
C language small project - address book (static version + dynamic version + file version)
Unity 之游戏特效
力扣刷题之数组序号计算(每日一题7/28)
C traps and defects Chapter 3 semantic "traps" 3.6 Boundary Calculation and asymmetric boundary
C traps and defects Chapter 3 semantic "traps" 3.9 integer overflow
原理知识用得上
Verilog: blocking assignment and non blocking assignment