import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class NMS6536 { private static Logger logger = LoggerFactory.getLogger(NMS6536.class); Map m_lastAccess = new HashMap(); Map m_measurementSets = new HashMap(); @Before public void setUp() throws Exception { Date oldDate = new Date(System.currentTimeMillis() - 500000); m_lastAccess.put("a", new Date()); m_lastAccess.put("b", oldDate); m_lastAccess.put("c", new Date()); m_measurementSets.put("a", new Date()); m_measurementSets.put("b", oldDate); m_measurementSets.put("c", new Date()); } @Test public void test() throws Exception { Assert.assertEquals(3, m_lastAccess.size()); Assert.assertEquals(3, m_measurementSets.size()); doHousekeeping(); Assert.assertEquals(2, m_lastAccess.size()); Assert.assertEquals(2, m_measurementSets.size()); Assert.assertFalse(m_lastAccess.containsKey("b")); Assert.assertFalse(m_measurementSets.containsKey("b")); } private synchronized void doHousekeeping() { final Iterator> it = m_lastAccess.entrySet().iterator(); while (it.hasNext()) { final Entry entry = it.next(); final Date lastAccess = entry.getValue(); final Date now = new Date(); if (now.getTime() - lastAccess.getTime() > 120000) { it.remove(); // removes entry from m_lastAccess to avoid ConcurrentModificationException m_measurementSets.remove(entry.getKey()); logger.warn("Timed out object removed '{}'", entry.getKey()); } } } }