当前位置:网站首页>Simple interest mode - lazy type
Simple interest mode - lazy type
2022-07-05 21:15:00 【Try to survive】
public class TestLazy {
public static void main(String[] args) {
Lazy l1 = Lazy.getInstance();
Lazy l2 = Lazy.getInstance();
System.out.println(l1 == l2);
System.out.println(l1);
System.out.println(l2);
}
}
public class TestLazy {
private static Lazy l1;
private static Lazy l2;
public static void main(String[] args) {
Thread t1 = new Thread(){
public void run(){
l1 = Lazy.getInstance();
}
};
Thread t2 = new Thread(){
public void run(){
l2 = Lazy.getInstance();
}
};
t1.start();
t2.start();
// The function of adding this code is , Guarantee l1 and l2 After the assignment is completed, compare
try {
t1.join();
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(l1);
System.out.println(l2);
System.out.println(l1 == l2);
}
}
class Lazy{
//(2) Use a static variable to hold this unique instance
private static Lazy instance;// There's no way to new
//(1) Constructor privatization
private Lazy(){
}
// Right
/* //(3) When you get this object , And then to create public synchronized static Lazy getInstance(){ if(instance == null){ instance = new Lazy(); } return instance; }*/
//(3) When you get this object , And then to create
public static Lazy getInstance(){
if(instance == null){
// The function of this external condition is to improve efficiency When this object has been created , So the thread behind , No matter how many , There is no need to wait for the lock
synchronized (Lazy.class) {
//Lazy.class Get the... Of the current class Class object
if(instance == null){
// The conditions are for safety
instance = new Lazy();
}
}
}
return instance;
}
}
public class TestLazy {
public static void main(String[] args) {
LazyDemo.test();
LazyDemo instance = LazyDemo.getInstance();
}
}
class LazyDemo{
//(1) Constructor privatization
private LazyDemo(){
}
// Inner class
private static class Inner{
//(2) Use a static constant of the static inner class to save this unique object
public static final LazyDemo INSTANCE = new LazyDemo();
static{
System.out.println(" Static code block of inner class ");
}
}
// When we call this method , Get external class object , Will lead to the loading and initialization of internal classes , Will lead to INSTANCE Object creation , So it also belongs to the lazy type
public static LazyDemo getInstance (){
return Inner.INSTANCE;
}
// To test whether the inner class is loaded
public static void test(){
System.out.println(" Static methods of external classes ");
}
}
边栏推荐
- Talk about my fate with some programming languages
- Postgres establish connection and delete records
- R language [data management]
- 让开发效率飞速提升的跨端方案
- wpf 获取datagrid 中指定行列的DataGridTemplateColumn中的控件
- 123456
- 显示屏DIN 4102-1 Class B1防火测试要求
- 显示器要申请BS 476-7 怎么送样?跟显示屏一样吗??
- 基于 Ingress Controller 在集群外访问 Zadig 自测环境(最佳实践)
- SYSTEMd resolved enable debug log
猜你喜欢

学习机器人无从下手?带你体会当下机器人热门研究方向有哪些

EN 438-7建筑覆盖物装饰用层压板材产品—CE认证

Comprehensive optimization of event R & D workflow | Erda version 2.2 comes as "7"

基于 Ingress Controller 在集群外访问 Zadig 自测环境(最佳实践)

EasyExcel的讀寫操作

【案例】定位的运用-淘宝轮播图

Clion-MinGW编译后的exe文件添加ico图标

Clickhouse copy paste multi line SQL statement error

显示屏DIN 4102-1 Class B1防火测试要求

面试官:并发编程实战会吗?(线程控制操作详解)
随机推荐
hdu2377Bus Pass(构建更复杂的图+spfa)
【案例】元素的显示与隐藏的运用--元素遮罩
Comprehensive optimization of event R & D workflow | Erda version 2.2 comes as "7"
[case] Application of element display and hiding -- element mask
Phpstudy Xiaopi's MySQL Click to start and quickly flash back. It has been solved
Is it necessary for bazel to learn
R language [data management]
显示屏DIN 4102-1 Class B1防火测试要求
[daily training] 729 My schedule I
CareerCup它1.8 串移包括问题
Longest swing sequence [greedy practice]
vant 源码解析 之深层 合并对象 深拷贝
当用户登录,经常会有实时的下拉框,例如,输入邮箱,将会@qq.com,@163.com,@sohu.com
Binary search
启牛2980有没有用?开户安全吗、
Establishment of terminal security capability verification environment and penetration test records
Teach yourself to train pytorch model to Caffe (I)
LeetCode_哈希表_困难_149. 直线上最多的点数
面试官:并发编程实战会吗?(线程控制操作详解)
Add ICO icon to clion MinGW compiled EXE file