ComponentTagProvider { public String getTag(String component, String containerId) { if (SliderUtils.isSet(component) && SliderUtils.isSet(containerId)) { ConcurrentHashMap<String, String> compTags = getComponentSpecificTags(component); synchronized (compTags) { for (String key : compTags.keySet()) { if (compTags.get(key).equals(containerId)) { return key; } } for (String key : compTags.keySet()) { if (compTags.get(key).equals(FREE)) { compTags.put(key, containerId); return key; } } String newKey = new Integer(compTags.size() + 1).toString(); compTags.put(newKey, containerId); return newKey; } } return ""; } ComponentTagProvider(); void recordAssignedTag(String component, String containerId, String tag); String getTag(String component, String containerId); void releaseTag(String component, String containerId);  }
@Test public void testTagProviderWithThread() throws Exception { ComponentTagProvider ctp = new ComponentTagProvider(); Thread thread = new Thread(new Taggged(ctp)); Thread thread2 = new Thread(new Taggged(ctp)); Thread thread3 = new Thread(new Taggged(ctp)); thread.start(); thread2.start(); thread3.start(); ctp.getTag("comp1", "cid50"); thread.join(); thread2.join(); thread3.join(); Assert.assertEquals("101", ctp.getTag("comp1", "cid101")); }