1

I can't figure out why i'm getting a ConcurrentModificationException on my "purge loop".

private HashMap<String, Long> firsthits = new HashMap<String, Long>();

public void addHit(String name) {
    firsthits.put(name, System.currentTimeMillis());

    if (firsthits.size() > 5) {
        for(Iterator<Map.Entry<String, Long>> it = firsthits.entrySet().iterator(); it.hasNext(); ) {

            Map.Entry<String, Long> s = it.next();

            if ( (s.getValue() + 15000) < System.currentTimeMillis() ) {
                firsthits.remove(s.getKey());
            }
        }
    }
}

I've tried many other loops aswell. And this is running 100% sync, no other threads touch firsthits.

The error points to line #14, which is if (firsthits.size() > 5) {

Here is the exception:

Caused by: java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextEntry(HashMap.java:839) ~[?:1.7.0_67]
at java.util.HashMap$EntryIterator.next(HashMap.java:880) ~[?:1.7.0_67]
at java.util.HashMap$EntryIterator.next(HashMap.java:878) ~[?:1.7.0_67]
TacoB
  • 71
  • 9

0 Answers0