Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. public class DoubleCheckLockingSingleton {
  2.  
  3. private static volatile DoubleCheckLockingSingleton singleton;
  4.  
  5. private DoubleCheckLockingSingleton() {} //private constructor
  6.  
  7. private static DoubleCheckLockingSingleton getInstance() {
  8.  
  9. if (null == singleton) { // if no instance is created we create one
  10.  
  11. synchronized (DoubleCheckLockingSingleton.class) { // we use synchronized block to make it thread safe
  12. if (null == singleton) { // we check again to be sure that no other thread initialized this variable
  13. singleton = new DoubleCheckLockingSingleton();
  14. }
  15. }
  16. }
  17.  
  18. return singleton;
  19. }
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement