• (2 Pages)
  • +
  • 1
  • 2

Phobos - A JavaFX Games Engine: Part 1 - Intro to Threading and DP Rate Topic: ***-- 2 Votes

#16 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 874
  • View blog
  • Posts: 2,706
  • Joined: 29-July 11

Posted 30 April 2014 - 08:06 PM

That is an absurd example.

This post has been edited by farrell2k: 30 April 2014 - 08:06 PM

Was This Post Helpful? 0
  • +
  • -

#17 arick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 14-August 16

Posted 15 August 2016 - 04:56 PM

View Postpryogene, on 27 April 2014 - 12:24 PM, said:

Phobos - A JavaFX Games Engine
Part 1: An Introduction to Software Design Patterns and Threading


public class Singleton {
    private static volatile Singleton _instance = null;

    private Singleton() { }

    public static Singleton getInstance() {
        if (_instance == null) {
            synchronized (Singleton.class) {
                if (_instance == null) {                
                    _instance = new Singleton();
                }
            }
        }
        return _instance;
    }
}



The naked "double-checked locking" anti-pattern that you are using makes your use of the Singleton pattern seem almost canonical by comparison. If you follow the link that you gave earlier, there are three idiom's quoted, in your reference, that demonstrate how to use double-checked locking pattern properly. It should be noted that you chose to use none of these techniques. The use of volatile helps. However, as a minimum, Bloch and others recommend that you should assign the _instance variable to a local variable, rather than use the _instance throughout. The reason for this recommendation is so that you are paying the cost of accessing the volatile object once.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2