📜 ⬆️ ⬇️

Java Singleton Option

After examining the solutions proposed in the articles “Right Singleton in Java” and “Implementing Singleton in JAVA” and “looking for brains”, I assumed that I could imagine two more similar variants of creating Singleton, practically devoid of many of the drawbacks of those solutions which were set forth earlier in the mentioned articles. But I want to start with the formulation of tasks, the solution of which will determine whether we have achieved the desired result.

Requirements:
1. Thread Safety
2. Serializability of changes to the singleton object reference
3. Controllability of creating an object in a try-catch block
4. Creating a Singleton Object Outside of a Constructor
5. Non-serializable retrieval of a reference to a singleton object, providing better performance.
6. “Lazy” object initialization — Singleton


Option 1
 public class Singleton { private static volatile Singleton instance; static { System.out.print("Singleton class Initializator\r\n"); try { InitSingleton(); } catch (Exception e) { System.out.print("Exception occurred\r\n"); } } private static synchronized void InitSingleton() { if (instance == null) instance = new Singleton(); } public Singleton() { System.out.print("Singleton has been initialized\r\n"); } public static Singleton getInstance() { return instance; } } 


In the proposed version, the call to the synchronized method to create a link to the object comes from the so-called static object initializer, which, in essence, runs at the same level as the initial member variables of the object, runs before the constructor is called, and must be serialized “by design ". But since my hope for the initializer block's self-serializability is not great, I provided a static link to the Singleton object with a volatile modifier ensuring consistency of static variable values ​​in different threads, and the static method of creating a Singleton object provided a synchronized modifier that guarantees the simultaneous execution of this method in one thread.
')
The second variant is similar to the first one in that it uses the same initialization principle - from the initializer block, but is distinguished by the absence of the volatile modifier in the static reference to the Singleton object for performance reasons, and the synchronization of the object creation is based on blocking the class member object with the previous initialization . At the same time, the idea proposed in the comments to the article “Right Singleton in Java” was used .

Option 2
 public class Singleton { private static Singleton instance; private static final Object lock = new Object(); private static boolean isInitialized = false; static { System.out.print("Singleton class Initializator\r\n"); try { synchronized (lock) { if (!isInitialized) if (instance == null) { instance = new Singleton(); isInitialized = true; } } } catch (Exception e) { System.out.print("Exception occurred\r\n"); } } public Singleton() { System.out.print("Singleton has been initialized\r\n"); } public static Singleton getInstance() { return instance; } } 

Source: https://habr.com/ru/post/134637/


All Articles