File tree Expand file tree Collapse file tree
main/java/pl/mperor/lab/java/design/pattern/creational/singleton
test/java/pl/mperor/lab/java/design/pattern/creational/singleton Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package pl .mperor .lab .java .design .pattern .creational .singleton ;
2+
3+ public class LazyInitializedSingleton {
4+
5+ private static LazyInitializedSingleton instance ;
6+ private final long time = System .currentTimeMillis ();
7+
8+ private LazyInitializedSingleton () {
9+ }
10+
11+ public static synchronized LazyInitializedSingleton getInstance () {
12+ if (instance == null ) {
13+ instance = new LazyInitializedSingleton ();
14+ }
15+ return instance ;
16+ }
17+
18+ public long getTime () {
19+ return time ;
20+ }
21+
22+ }
Original file line number Diff line number Diff line change 1+ package pl .mperor .lab .java .design .pattern .creational .singleton ;
2+
3+ public class Singleton {
4+
5+ private static final Singleton instance = new Singleton ();
6+ private final long time = System .currentTimeMillis ();
7+
8+ private Singleton () {
9+ }
10+
11+ public static Singleton getInstance () {
12+ return instance ;
13+ }
14+
15+ public long getTime () {
16+ return time ;
17+ }
18+
19+ }
Original file line number Diff line number Diff line change 1+ package pl .mperor .lab .java .design .pattern .creational .singleton ;
2+
3+ import org .junit .jupiter .api .Assertions ;
4+ import org .junit .jupiter .api .Test ;
5+
6+ import java .util .concurrent .CompletableFuture ;
7+
8+ public class LazyInitializedSingletonTest {
9+
10+ @ Test
11+ public void shouldOnlyAllowToLazyCreateOneInstanceOfSingleton () {
12+ var first = CompletableFuture .supplyAsync (() -> {
13+ System .out .println ("First ..." );
14+ return LazyInitializedSingleton .getInstance ();
15+ });
16+ var second = CompletableFuture .supplyAsync (() -> {
17+ System .out .println ("Second ..." );
18+ return LazyInitializedSingleton .getInstance ();
19+ });
20+
21+ LazyInitializedSingleton firstResult = first .join ();
22+ LazyInitializedSingleton secondResult = second .join ();
23+
24+ Assertions .assertSame (firstResult , secondResult );
25+ Assertions .assertEquals (firstResult .getTime (), secondResult .getTime ());
26+ }
27+ }
Original file line number Diff line number Diff line change 1+ package pl .mperor .lab .java .design .pattern .creational .singleton ;
2+
3+ import org .junit .jupiter .api .Assertions ;
4+ import org .junit .jupiter .api .Test ;
5+
6+ import java .util .concurrent .TimeUnit ;
7+
8+ public class SingletonTest {
9+
10+ @ Test
11+ public void shouldOnlyAllowToCreateOneInstanceOfSingleton () throws InterruptedException {
12+ var instance = Singleton .getInstance ();
13+ long time = instance .getTime ();
14+
15+ TimeUnit .MILLISECONDS .sleep (50 );
16+ long timeAfterBreak = Singleton .getInstance ().getTime ();
17+
18+ Assertions .assertSame (instance , Singleton .getInstance ());
19+ Assertions .assertEquals (time , timeAfterBreak );
20+ }
21+
22+ }
You can’t perform that action at this time.
0 commit comments