drSdGdBy

lazy singleton

Jan 16th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.39 KB | None | 0 0
  1. public class Singleton {
  2.         private static volatile Singleton instance;
  3.    
  4.         public static Singleton getInstance() {
  5.         Singleton localInstance = instance;
  6.         if (localInstance == null) {
  7.             synchronized (Singleton.class) {
  8.                 localInstance = instance;
  9.                 if (localInstance == null) {
  10.                     instance = localInstance = new Singleton();
  11.                 }
  12.             }
  13.         }
  14.         return localInstance;
  15.     }
  16. }
Add Comment
Please, Sign In to add comment