File tree Expand file tree Collapse file tree 4 files changed +69
-1
lines changed
singleton-double-checked-locking
src/main/java/com/ibrahimatay Expand file tree Collapse file tree 4 files changed +69
-1
lines changed Original file line number Diff line number Diff line change @@ -12,7 +12,8 @@ Examples of design patterns in Java is presented in a way that is easy for human
12
12
- Singleton
13
13
- [ Eager Instantiation] ( /singleton-eager-instantiation/src/main/java/com/ibrahimatay/Main.java )
14
14
- [ 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 )
16
17
17
18
## *** Structural***
18
19
- [ Adapter] ( /adapter/src/main/java/com/ibrahimatay/Main.java )
Original file line number Diff line number Diff line change 36
36
<module >pipeline</module >
37
37
<module >singleton-static-block</module >
38
38
<module >singleton-thread-safe</module >
39
+ <module >singleton-double-checked-locking</module >
39
40
</modules >
40
41
41
42
<properties >
Original file line number Diff line number Diff line change
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 >
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments