Skip to content

Commit dd10f2b

Browse files
committed
singleton-double-checked-locking
1 parent b600ee9 commit dd10f2b

File tree

4 files changed

+69
-1
lines changed

4 files changed

+69
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ Examples of design patterns in Java is presented in a way that is easy for human
1212
- Singleton
1313
- [Eager Instantiation](/singleton-eager-instantiation/src/main/java/com/ibrahimatay/Main.java)
1414
- [Lazy Instantiation](/singleton-lazy-instantiation/src/main/java/com/ibrahimatay/Main.java),
15-
- [Enum-based Singleton](/singleton-with-enum/src/main/java/com/ibrahimatay/Main.java)
15+
- [Enum-based Singleton](/singleton-with-enum/src/main/java/com/ibrahimatay/Main.java)
16+
- [Double Checked Locking](/singleton-double-checked-locking/src/main/java/com/ibrahimatay/Main.java)
1617

1718
## ***Structural***
1819
- [Adapter](/adapter/src/main/java/com/ibrahimatay/Main.java)

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
<module>pipeline</module>
3737
<module>singleton-static-block</module>
3838
<module>singleton-thread-safe</module>
39+
<module>singleton-double-checked-locking</module>
3940
</modules>
4041

4142
<properties>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>com.ibrahimatay</groupId>
8+
<artifactId>design-patterns</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
</parent>
11+
12+
<artifactId>singleton-double-checked-locking</artifactId>
13+
14+
<properties>
15+
<maven.compiler.source>23</maven.compiler.source>
16+
<maven.compiler.target>23</maven.compiler.target>
17+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18+
</properties>
19+
20+
</project>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.ibrahimatay;
2+
3+
import java.text.SimpleDateFormat;
4+
import java.util.Date;
5+
import java.util.List;
6+
import java.util.stream.IntStream;
7+
8+
public class Main {
9+
public static void main(String[] args) {
10+
List<String> threads = List.of("Thread-1", "Thread-2", "Thread-3");
11+
12+
// Using Java Streams to run logging in parallel
13+
threads.parallelStream().forEach(threadName -> {
14+
Singleton logger = Singleton.getInstance();
15+
IntStream.rangeClosed(1, 100).forEach(i -> {
16+
logger.log(threadName + " - Log message " + i);
17+
try {
18+
Thread.sleep(100); // Simulating delay
19+
} catch (InterruptedException e) {
20+
e.printStackTrace();
21+
}
22+
});
23+
});
24+
}
25+
}
26+
27+
class Singleton {
28+
private static volatile Singleton instance;
29+
private Singleton(){}
30+
31+
public static Singleton getInstance(){
32+
if(instance==null){
33+
synchronized (Singleton.class){
34+
if (instance==null){
35+
instance = new Singleton();
36+
}
37+
}
38+
}
39+
return instance;
40+
}
41+
42+
public void log(String message) {
43+
String timeStamp = new SimpleDateFormat("HH:mm:ss.SSS").format(new Date());
44+
System.out.println(timeStamp + " - " + message);
45+
}
46+
}

0 commit comments

Comments
 (0)