Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@yenerm
Last active May 4, 2020 16:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yenerm/a74ab9f830fa108618e090f218e8c78e to your computer and use it in GitHub Desktop.
Save yenerm/a74ab9f830fa108618e090f218e8c78e to your computer and use it in GitHub Desktop.
Double locking singleton
<!-- Copyright 2019 Google LLC.
SPDX-License-Identifier: Apache-2.0 -->
public class Singleton{
private static Singleton INSTANCE;
private Singleton(){}
public static Singleton getInstance(){
if (INSTANCE == null) { // Single Checked
synchronized (Singleton.class) {
if (INSTANCE == null) { // Double checked
INSTANCE = new Singleton();
}
}
}
return INSTANCE;
}
private int count = 0;
public int count(){ return count++; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment