Fix you
싱글톤 (DCL) 본문
Doble Checking Locking 싱글톤 예제
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | package com.frjufvjn.netty.websocket; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; /** * @author PJW * @description 채널 그룹 관리 맵 싱글톤 객체 DCL (Double Checking Locking) */ public class UserChannelMap { private UserChannelMap() {} private volatile static ConcurrentMap<String, Set<Object>> UserChannelMap; public static synchronized ConcurrentMap<String, Set<Object>> getInsatance() { if(UserChannelMap == null) { synchronized(UserChannelMap.class) { if(UserChannelMap == null) { UserChannelMap = new ConcurrentHashMap<String, Set<Object>>(); } } } return UserChannelMap; } } | cs |
Lazy Holder
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public class Singleton { private volatile static Singleton instance; private Singeton() {} public static Singleton getInstance() { if (instance == null) { synchronized(Singleton.class) { if (instance == null) { instance == new Singleton(); } } } return instance; } } | cs |