Skip to content

Instantly share code, notes, and snippets.

View saviomisael's full-sized avatar

Savio Misael saviomisael

View GitHub Profile
@saviomisael
saviomisael / Singleton.java
Last active August 30, 2024 19:22
Singleton Thread-Safe Java
import java.util.concurrent.locks.ReentrantLock;
public class Singleton {
private ReentrantLock lock = new ReentrantLock();
private Singleton instance = null;
public Singleton getInstance() {
// this creates an auxiliary variable to prevent another thread wait the synchronized block gets unlock
Singleton result = instance;