-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSingletonLazy.java
More file actions
39 lines (33 loc) · 1.1 KB
/
SingletonLazy.java
File metadata and controls
39 lines (33 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package org.cp;
/**
* 单例模式 懒汉式
* create by CP on 2019/7/25 0025.
*/
class SingletonLazy {
private SingletonLazy(){}
private static SingletonLazy singletonLazy;
static SingletonLazy getInstance() {
if (singletonLazy==null) {//高效判断,不用每次都进来锁里面,
synchronized (SingletonLazy.class) {//lock it, for safe
if (singletonLazy==null) {
try {
Thread.sleep(100);//延迟看效果
} catch (InterruptedException e) {
e.printStackTrace();
}
singletonLazy = new SingletonLazy();
}
}
}
System.out.println(singletonLazy);//引用地址一样表示线程安全
return singletonLazy;
}
}
class SingletonLazyTest{
public static void main(String[] args) {
Thread thread1 = new Thread(() -> SingletonLazy.getInstance());
Thread thread2 = new Thread(() -> SingletonLazy.getInstance());
thread1.start();
thread2.start();
}
}