The Singleton Pattern ensures a class has only one instance that provides a global point to access it. This design pattern is perfect for maintaining a set of configurations or other variables always available at run-time in your software.
public static Singleton getInstance() {
if (uniqueInstance == null) {
uniqueInstance = new Singleton();
}
return uniqueInstance;
}
In this code snippet, if you have a multithreaded application that executes the same class, some concurrency problems can occur. For instance if the two threads accesses to the condition at the same time, creates two different Singleton instances. How can you fix this problem? Newer virtual machines (like JVM 1.5) ensures this when you create the Singleton instance in a static variable outside of any method. The JVM guarantees that the instance will be created before any thread accesses the static uniqueInstance variable. The solution for this is:
public class Singleton {
private static Singleton uniqueInstance = new Singleton ();
private Singleton() {}
public static Singleton getInstance () {
return uniqueInstance;
}
}
This rule says that 80% of your outcomes come from 20% of your inputs. For instance, in your life, there are certain activities that you do (your 20%) that account for the majority (your 80%) of your happiness and outputs.
