Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
* Using ThreadLocal incorrectly can lead to memory leaks or unexpected behavior.
* ThreadLocal variables are meant to provide thread-specific storage that each thread can independently access,
* but improper usage or not cleaning up properly can cause problems.
*
*
* <p>
* In this example, the ThreadLocal variable is used to store and retrieve values specific to each thread.
* However, the values are not removed after usage, which can lead to memory leaks,
* especially in environments where threads are reused, such as in thread pools.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
/**
*
* Another approach is to use double-checked locking with the volatile keyword to minimize synchronization overhead.
*
* <p>
* In this version, the volatile keyword ensures visibility of changes to the instance variable across threads,
* while double-checked locking minimizes synchronization overhead.
*
*
* */
public class DoubleCheckedLockingSingleton {
private static volatile DoubleCheckedLockingSingleton instance;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package org.alxkm.antipatterns.lackofthreadsafetyinsingletons;


/**
*
* Alternative Resolution: Initialization-on-Demand Holder Idiom
* 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.
* <p>
* <p>
* In this version, the Holder class is loaded on the first invocation of getInstance(), ensuring thread safety through the class loader mechanism.
*
**/


public class HolderSingleton {
/**
* Private constructor to prevent instantiation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
/**
*
* To ensure thread safety, we can synchronize the getInstance method.
*
*
* <p>
* In this revised example, the getInstance method is synchronized,
* ensuring that only one instance is created even when multiple threads access the method simultaneously.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package org.alxkm.antipatterns.lackofthreadsafetyinsingletons;

/**
*
* A common issue with singletons is the lack of proper synchronization,
* which can lead to multiple instances being created in a multithreaded environment.
* <p>
* In this example, the getInstance method is not synchronized,
* which can lead to multiple instances being created if multiple threads access the method simultaneously.
*
*/
public class UnsafeSingleton {
private static UnsafeSingleton instance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
* When multiple threads try to access these methods simultaneously, they experience high contention, reducing performance.
*
*/

public class LockContentionExample {
private final Object lock = new Object();
private int counter = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.concurrent.locks.StampedLock;


/**
*
* Another approach is to use StampedLock, which allows for more flexible lock handling, including optimistic reads.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package org.alxkm.antipatterns.nonatomiccompoundactions;

/**
*
* To resolve this issue, we can synchronize the method to ensure that the compound action is atomic.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.concurrent.atomic.AtomicInteger;


/**
* Another approach is to use the atomic classes provided by the java.util.concurrent package,
* such as AtomicInteger, which provides atomic operations for integers.
Expand All @@ -12,6 +11,7 @@
* that the compound action is performed atomically.
* <p>
* This approach leverages the atomic classes in the java.util.concurrent package to provide thread safety without explicit synchronization.
*
*/
public class AtomicIntegerExample {
private final AtomicInteger counter = new AtomicInteger();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package org.alxkm.antipatterns.nonatomiccompoundactions;

/**
*
* Non-atomic compound actions occur when compound actions (e.g., check-then-act, read-modify-write) are performed without proper synchronization,
* leading to race conditions and incorrect results. Here's an example demonstrating this problem along with a resolution.
* <p>
* In this example, the incrementIfLessThan method performs a non-atomic compound action.
* If multiple threads execute this method concurrently, they may both pass the check before either increments the counter,
* leading to incorrect results.
*
*/

public class NonAtomicCompoundActionsExample {
private int counter = 0;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package org.alxkm.antipatterns.startingthreadinconstructor;

/**
* Starting a thread within a constructor can lead to unpredictable behavior or issues where the thread starts before the object is fully constructed.
*
* Starting a thread within a constructor can lead to unpredictable behavior or issues where the thread starts before the object is fully constructed.
*
* In this example, the ThreadInConstructor class starts the thread within the constructor.
* This can lead to issues where the thread starts before the object is fully initialized, potentially causing unexpected behavior.
*
*
*/
public class ThreadInConstructor extends Thread {
private final String message;
Expand Down