Skip to content

Commit 3126ad3

Browse files
authored
fix: Remove unnecessary and possibly not threadsafe flag (iluwatar#1811)
1 parent e2ebb59 commit 3126ad3

File tree

2 files changed

+2
-8
lines changed

2 files changed

+2
-8
lines changed

singleton/src/main/java/com/iluwatar/singleton/ThreadSafeDoubleCheckLocking.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,12 @@ public final class ThreadSafeDoubleCheckLocking {
3636

3737
private static volatile ThreadSafeDoubleCheckLocking instance;
3838

39-
private static boolean flag = true;
40-
4139
/**
4240
* private constructor to prevent client from instantiating.
4341
*/
4442
private ThreadSafeDoubleCheckLocking() {
4543
// to prevent instantiating by Reflection call
46-
if (flag) {
47-
flag = false;
48-
} else {
44+
if (instance != null) {
4945
throw new IllegalStateException("Already initialized.");
5046
}
5147
}

singleton/src/main/java/com/iluwatar/singleton/ThreadSafeLazyLoadedIvoryTower.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ public final class ThreadSafeLazyLoadedIvoryTower {
3434

3535
private ThreadSafeLazyLoadedIvoryTower() {
3636
// Protect against instantiation via reflection
37-
if (instance == null) {
38-
instance = this;
39-
} else {
37+
if (instance != null) {
4038
throw new IllegalStateException("Already initialized.");
4139
}
4240
}

0 commit comments

Comments
 (0)