Skip to content

Latest commit

 

History

History
33 lines (25 loc) · 880 Bytes

File metadata and controls

33 lines (25 loc) · 880 Bytes

Singleton

Requirement

  • Create an object on request
  • Thread safe
  • Small overhead on synchronization

Notes

Code

    public static Singleton getInstance() {
        if (singleton == null) {
            synchronized (Singleton.class) {
                if (singleton == null) {
                    singleton = new Singleton();
                    System.out.println("Singleton object created");
                }
            }
        }
        return singleton;
    }

return to main page