Skip to content

Commit 060b9c6

Browse files
authored
refactor: code examples cleanup, and javadocs (#60)
1 parent 1eb826d commit 060b9c6

File tree

11 files changed

+12
-16
lines changed

11 files changed

+12
-16
lines changed

src/main/java/org/alxkm/antipatterns/improperuseofthreadlocal/ThreadLocalExample.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
* Using ThreadLocal incorrectly can lead to memory leaks or unexpected behavior.
66
* ThreadLocal variables are meant to provide thread-specific storage that each thread can independently access,
77
* but improper usage or not cleaning up properly can cause problems.
8-
*
9-
*
8+
* <p>
109
* In this example, the ThreadLocal variable is used to store and retrieve values specific to each thread.
1110
* However, the values are not removed after usage, which can lead to memory leaks,
1211
* especially in environments where threads are reused, such as in thread pools.

src/main/java/org/alxkm/antipatterns/lackofthreadsafetyinsingletons/DoubleCheckedLockingSingleton.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
/**
44
*
55
* Another approach is to use double-checked locking with the volatile keyword to minimize synchronization overhead.
6-
*
6+
* <p>
77
* In this version, the volatile keyword ensures visibility of changes to the instance variable across threads,
88
* while double-checked locking minimizes synchronization overhead.
99
*
10-
*
1110
* */
1211
public class DoubleCheckedLockingSingleton {
1312
private static volatile DoubleCheckedLockingSingleton instance;

src/main/java/org/alxkm/antipatterns/lackofthreadsafetyinsingletons/HolderSingleton.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
package org.alxkm.antipatterns.lackofthreadsafetyinsingletons;
22

3-
43
/**
4+
*
55
* Alternative Resolution: Initialization-on-Demand Holder Idiom
66
* Another approach to implement a thread-safe singleton is the Initialization-on-Demand Holder idiom, which leverages the class loader mechanism to ensure thread safety.
77
* <p>
8-
* <p>
98
* In this version, the Holder class is loaded on the first invocation of getInstance(), ensuring thread safety through the class loader mechanism.
9+
*
1010
**/
11-
12-
1311
public class HolderSingleton {
1412
/**
1513
* Private constructor to prevent instantiation.

src/main/java/org/alxkm/antipatterns/lackofthreadsafetyinsingletons/SafeSingleton.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
/**
44
*
55
* To ensure thread safety, we can synchronize the getInstance method.
6-
*
7-
*
6+
* <p>
87
* In this revised example, the getInstance method is synchronized,
98
* ensuring that only one instance is created even when multiple threads access the method simultaneously.
109
*

src/main/java/org/alxkm/antipatterns/lackofthreadsafetyinsingletons/UnsafeSingleton.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package org.alxkm.antipatterns.lackofthreadsafetyinsingletons;
22

33
/**
4+
*
45
* A common issue with singletons is the lack of proper synchronization,
56
* which can lead to multiple instances being created in a multithreaded environment.
67
* <p>
78
* In this example, the getInstance method is not synchronized,
89
* which can lead to multiple instances being created if multiple threads access the method simultaneously.
10+
*
911
*/
1012
public class UnsafeSingleton {
1113
private static UnsafeSingleton instance;

src/main/java/org/alxkm/antipatterns/lockcontention/LockContentionExample.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
* When multiple threads try to access these methods simultaneously, they experience high contention, reducing performance.
1010
*
1111
*/
12-
1312
public class LockContentionExample {
1413
private final Object lock = new Object();
1514
private int counter = 0;

src/main/java/org/alxkm/antipatterns/lockcontention/StampedLockExample.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.util.concurrent.locks.StampedLock;
44

5-
65
/**
76
*
87
* Another approach is to use StampedLock, which allows for more flexible lock handling, including optimistic reads.

src/main/java/org/alxkm/antipatterns/nonatomiccompoundactions/AtomicCompoundActionsExample.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
package org.alxkm.antipatterns.nonatomiccompoundactions;
2+
23
/**
34
*
45
* To resolve this issue, we can synchronize the method to ensure that the compound action is atomic.

src/main/java/org/alxkm/antipatterns/nonatomiccompoundactions/AtomicIntegerExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.util.concurrent.atomic.AtomicInteger;
44

5-
65
/**
76
* Another approach is to use the atomic classes provided by the java.util.concurrent package,
87
* such as AtomicInteger, which provides atomic operations for integers.
@@ -12,6 +11,7 @@
1211
* that the compound action is performed atomically.
1312
* <p>
1413
* This approach leverages the atomic classes in the java.util.concurrent package to provide thread safety without explicit synchronization.
14+
*
1515
*/
1616
public class AtomicIntegerExample {
1717
private final AtomicInteger counter = new AtomicInteger();

src/main/java/org/alxkm/antipatterns/nonatomiccompoundactions/NonAtomicCompoundActionsExample.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package org.alxkm.antipatterns.nonatomiccompoundactions;
22

33
/**
4+
*
45
* Non-atomic compound actions occur when compound actions (e.g., check-then-act, read-modify-write) are performed without proper synchronization,
56
* leading to race conditions and incorrect results. Here's an example demonstrating this problem along with a resolution.
67
* <p>
78
* In this example, the incrementIfLessThan method performs a non-atomic compound action.
89
* If multiple threads execute this method concurrently, they may both pass the check before either increments the counter,
910
* leading to incorrect results.
11+
*
1012
*/
11-
1213
public class NonAtomicCompoundActionsExample {
1314
private int counter = 0;
1415

0 commit comments

Comments
 (0)