@Test public void testGetOrCreateGroupMultipleThread() throws Exception { final String namespace = "test-namespace"; int threadCount = 32; final List<ConnectionGroup> groups = new CopyOnWriteArrayList<>(); final CountDownLatch latch = new CountDownLatch(threadCount); for (int i = 0; i < threadCount; i++) { new Thread(new Runnable() { @Override public void run() { groups.add(ConnectionManager.getOrCreateGroup(namespace)); latch.countDown(); } }).start(); } latch.await(); for (int i = 1; i < groups.size(); i++) { assertSame(groups.get(i - 1).getNamespace(), groups.get(i).getNamespace()); } }
public static ConnectionGroup getOrCreateGroup(String namespace) { AssertUtil.assertNotBlank(namespace, "namespace should not be empty"); ConnectionGroup group = CONN_MAP.get(namespace); if (group == null) { synchronized (CREATE_LOCK) { if ((group = CONN_MAP.get(namespace)) == null) { group = new ConnectionGroup(namespace); CONN_MAP.put(namespace, group); } } } return group; }
ConnectionManager { public static ConnectionGroup getOrCreateGroup(String namespace) { AssertUtil.assertNotBlank(namespace, "namespace should not be empty"); ConnectionGroup group = CONN_MAP.get(namespace); if (group == null) { synchronized (CREATE_LOCK) { if ((group = CONN_MAP.get(namespace)) == null) { group = new ConnectionGroup(namespace); CONN_MAP.put(namespace, group); } } } return group; } }
ConnectionManager { public static ConnectionGroup getOrCreateGroup(String namespace) { AssertUtil.assertNotBlank(namespace, "namespace should not be empty"); ConnectionGroup group = CONN_MAP.get(namespace); if (group == null) { synchronized (CREATE_LOCK) { if ((group = CONN_MAP.get(namespace)) == null) { group = new ConnectionGroup(namespace); CONN_MAP.put(namespace, group); } } } return group; } private  ConnectionManager(); }
ConnectionManager { public static ConnectionGroup getOrCreateGroup(String namespace) { AssertUtil.assertNotBlank(namespace, "namespace should not be empty"); ConnectionGroup group = CONN_MAP.get(namespace); if (group == null) { synchronized (CREATE_LOCK) { if ((group = CONN_MAP.get(namespace)) == null) { group = new ConnectionGroup(namespace); CONN_MAP.put(namespace, group); } } } return group; } private  ConnectionManager(); static int getConnectedCount(String namespace); static ConnectionGroup getOrCreateGroup(String namespace); static void removeConnection(String address); static void removeConnection(String namespace, String address); static ConnectionGroup addConnection(String namespace, String address); static ConnectionGroup getOrCreateConnectionGroup(String namespace); static ConnectionGroup getConnectionGroup(String namespace); }
ConnectionManager { public static ConnectionGroup getOrCreateGroup(String namespace) { AssertUtil.assertNotBlank(namespace, "namespace should not be empty"); ConnectionGroup group = CONN_MAP.get(namespace); if (group == null) { synchronized (CREATE_LOCK) { if ((group = CONN_MAP.get(namespace)) == null) { group = new ConnectionGroup(namespace); CONN_MAP.put(namespace, group); } } } return group; } private  ConnectionManager(); static int getConnectedCount(String namespace); static ConnectionGroup getOrCreateGroup(String namespace); static void removeConnection(String address); static void removeConnection(String namespace, String address); static ConnectionGroup addConnection(String namespace, String address); static ConnectionGroup getOrCreateConnectionGroup(String namespace); static ConnectionGroup getConnectionGroup(String namespace);  }