当前位置:网站首页>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 ");
}
}
边栏推荐
- selenium 查找b或p标签的内容
- The transformation based on vertx web sstore redis to realize the distributed session of vertx HTTP application
- Viewrootimpl and windowmanagerservice notes
- 模式-“里氏替换原则”
- Utils/index TS tool function
- 第05章_存储引擎
- 基于vertx-web-sstore-redis的改造实现vertx http应用的分布式session
- Careercup its 1.8 serial shift includes problems
- Display DIN 4102-1 Class B1 fire test requirements
- Matplotlib drawing retouching (how to form high-quality drawings, such as how to set fonts, etc.)
猜你喜欢

第05章_存储引擎

基于flask写一个接口

2022-07-03-CKA-粉丝反馈最新情况

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

Using webassembly to operate excel on the browser side

MySQL 千万数据量深分页优化, 拒绝线上故障!

使用WebAssembly在浏览器端操作Excel

Clickhouse copy paste multi line SQL statement error

The transformation based on vertx web sstore redis to realize the distributed session of vertx HTTP application

PVC plastic sheets BS 476-6 determination of flame propagation properties
随机推荐
Introduction of ArcGIS grid resampling method
bazel是否有学习的必要
Écrire une interface basée sur flask
Establishment of terminal security capability verification environment and penetration test records
张丽俊:穿透不确定性要靠四个“不变”
shell编程100例
木板ISO 5660-1 热量释放速率摸底测试
XML modeling
序列联配Sequence Alignment
请查收.NET MAUI 的最新学习资源
systemd-resolved 开启 debug 日志
postgres 建立连接并删除记录
驱动壳美国测试UL 2043 符合要求有哪些?
Web Service简单入门示例
CareerCup它1.8 串移包括问题
面试官:并发编程实战会吗?(线程控制操作详解)
Aitm2-0002 12s or 60s vertical combustion test
终端安全能力验证环境搭建和渗透测试记录
EN 438-7建筑覆盖物装饰用层压板材产品—CE认证
Monorepo management methodology and dependency security