-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingleton.java
More file actions
39 lines (31 loc) · 1.15 KB
/
Singleton.java
File metadata and controls
39 lines (31 loc) · 1.15 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 DesignPatterns;
import java.util.ArrayList;
import java.util.List;
public class Singleton {
private final static int MAX_CAPACITY = 10_000;
public static void main(String[] args) throws InterruptedException {
List<Thread> threads = new ArrayList<>(MAX_CAPACITY);
var startTime = System.currentTimeMillis();
for (int i = 0; i < MAX_CAPACITY; i++) {
threads.add(Thread.ofVirtual().start(() -> {
SingletonInstance singletonInstance = SingletonInstance.getInstance();
}));
}
var endTime = System.currentTimeMillis();
System.out.printf("it took %dms to execute.\n", endTime - startTime);
}
}
class SingletonInstance {
private static volatile SingletonInstance singletonInstance;
private SingletonInstance() {}
public static SingletonInstance getInstance() {
if (singletonInstance == null) {
synchronized (SingletonInstance.class) {
if (singletonInstance == null) {
singletonInstance = new SingletonInstance();
}
}
}
return singletonInstance;
}
}