Is singleton thread-safe in Java?

Adelaide 103 Published: 07/30/2024

Is singleton thread-safe in Java?

The infamous singleton pattern!

In Java, the singleton design pattern is typically implemented by creating a class with a private constructor and a public static method to retrieve the single instance of the class. The idea is that there's only one instance of this class in the entire application.

Now, when it comes to thread-safety, things get a bit more complicated. In general, the singleton pattern itself doesn't guarantee thread-safety, as multiple threads may still attempt to create separate instances of the same class, potentially leading to unintended consequences.

To illustrate this, consider the following (naive) implementation:

public class Singleton {

private static Singleton instance;

public static Singleton getInstance() {

if (instance == null) {

instance = new Singleton(); // Thread-unsafe!

}

return instance;

}

private Singleton() {} // Private constructor to prevent instantiation

}

In this implementation, the getInstance() method creates a new instance of the singleton class whenever it's called and no instance has been created yet. However, if multiple threads call getInstance() concurrently, each thread may create its own separate instance, effectively violating the singleton principle!

To address this issue, you can use various synchronization techniques to ensure that only one instance is created, even in a multi-threaded environment. Here are a few strategies:

Double-checked locking: This approach uses synchronized blocks and a lazy initialization mechanism to prevent multiple threads from creating separate instances.
public class Singleton {

private static volatile Singleton instance;

public static synchronized Singleton getInstance() {

if (instance == null) {

instance = new Singleton(); // Synchronized!

}

return instance;

}

private Singleton() {} // Private constructor to prevent instantiation

}

Initialization-on-demand holders: This technique uses a nested class with a private static holder instance that's initialized only when the outer class's getInstance() method is called.
public class Singleton {

private Singleton() {} // Private constructor to prevent instantiation

public static Singleton getInstance() {

return Holder.INSTANCE;

}

private static class Holder {

private static final Singleton INSTANCE = new Singleton();

}

}

Bill Pugh singleton: This approach uses a static nested class with an enum-based initialization mechanism, which ensures that only one instance is created.
public enum Singleton {

INSTANCE;

public static Singleton getInstance() {

return INSTANCE;

}

private Singleton() {} // Private constructor to prevent instantiation

}

In summary, while the singleton pattern itself doesn't guarantee thread-safety, you can use various synchronization techniques, such as double-checked locking, initialization-on-demand holders, or Bill Pugh's approach, to ensure that only one instance is created in a multi-threaded environment.

I hope this helps clarify the importance of thread-safety when implementing singletons in Java!

Java singleton example

Here is a basic example of how you could implement a Singleton class in Java:

public class Singleton {

// Private constructor to prevent instantiation from outside the class

private Singleton() {}

// Static instance variable for holding the single instance

private static Singleton uniqueInstance;

// Static method for getting the single instance

public static Singleton getInstance() {

if (uniqueInstance == null) {

synchronized (Singleton.class) { // Synchronizing access to the instance

if (uniqueInstance == null) {

uniqueInstance = new Singleton(); // Creating a new instance if none exists

}

}

}

return uniqueInstance;

}

public void doSomething() {

System.out.println("I am doing something");

}

public static void main(String[] args) {

Singleton singleton1 = Singleton.getInstance();

Singleton singleton2 = Singleton.getInstance();

// Both instances should be the same

if (singleton1 == singleton2) {

System.out.println("Both are the same instance");

} else {

System.out.println("They are different instances");

}

singleton1.doSomething();

}

}

In this example, we create a Singleton class that has a private constructor to prevent instantiation from outside the class. It also has a static instance variable uniqueInstance for holding the single instance of the Singleton.

The getInstance() method is used to get the single instance. If no instance exists (i.e., uniqueInstance == null), it creates a new one and assigns it to uniqueInstance. This ensures that only one instance is created, regardless of how many times you call getInstance().

In the main() method, we test if two calls to getInstance() return the same instance. Since they are singletons, they should be the same instance.

This example does not use lazy initialization (i.e., creating the instance only when it is first needed). This can be useful for large or complex instances that take a long time to create. To implement lazy initialization in this Singleton class, you would add a check in getInstance() for whether an instance already exists before creating a new one:

public static Singleton getInstance() {

if (uniqueInstance == null) {

synchronized (Singleton.class) { // Synchronizing access to the instance

if (uniqueInstance == null) {

uniqueInstance = new Singleton(); // Creating a new instance if none exists

}

}

}

return uniqueInstance;

}

However, using lazy initialization with synchronization can be problematic because it may cause performance issues. A better approach might be to use double-checked locking:

public static Singleton getInstance() {

if (uniqueInstance == null) {

synchronized (Singleton.class) { // Synchronizing access to the instance

if (uniqueInstance == null) {

uniqueInstance = new Singleton(); // Creating a new instance if none exists

}

}

}

return uniqueInstance;

}

However, this approach is not thread-safe in all Java environments. Therefore, it's generally best to use lazy initialization without synchronization:

private static class SingletonHelper {

public static Singleton getInstance() {

if (uniqueInstance == null) {

uniqueInstance = new Singleton(); // Creating a new instance if none exists

}

return uniqueInstance;

}

private SingletonHelper() {}

}

public static Singleton getInstance() {

return SingletonHelper.getInstance();

}

In this approach, uniqueInstance is only initialized when it's first needed.