public static MyClass {
    private static volatile ProcessManager singleton = null;
    
    public static ProcessManager getInstance() throws Exception {
        if (singleton == null) {
            synchronized (MyClass.class) {
                if (singleton == null) {
                    singleton = new ProcessManager();
                }
            }
        }
        return singleton;
    }
}

The double-checking was invented prior to Java5.

The purpose is if the field isn't null, you don't have to synchronize. But since the java memory model specification was cleaned up and Synchronize/Volatile were given much better specification and semantics it is totally useless boilerplate that you should only write if you have to support Java4. There is a Google Tech Talk that covers this.

By cody, 2017-12-12 08:02:01