Adam Bien's Weblog
Endless Loops In Unsychronized WeakHashMap
Unsynchronized access to java.util.WeakHashMap may cause …an endless loop.
See method java.util.WeakHashMap#get:
public V get(Object key) {
Object k = maskNull(key);
int h = hash(k);
Entry[] tab = getTable();
int index = indexFor(h, tab.length);
Entry e = tab[index];
while (e != null) {
if (e.hash == h && eq(k, e.get()))
return e.value;
e = e.next;
}
return null;
}
Although it seems unlikely, it actually happens. One day we found a majority of worker threads stuck with the following stacktrace:
"httpSSLWorkerThread-32470-8"
java.lang.Thread.State: RUNNABLE
at java.util.WeakHashMap.get(WeakHashMap.java:355)
at javax.faces.component.UIComponentBase.populateDescriptorsMapIfNecessary(UIComponentBase.java:147)
at javax.faces.component.UIComponentBase.(UIComponentBase.java:142)
at javax.faces.component.UIOutput.(UIOutput.java:119)
See you at Java EE Workshops at MUC Airport (March 25th-28th)!
Posted at 07:24AM Oct 24, 2012 by Adam Bien in Real World Java EE Patterns - Rethinking Best Practices | Comments[3] | Views/Hits: 3288
NEW Workshop: "JPA, NoSQL, Caching, Grids and Distributed Caches with Java EE 7", May 7th, 2013, Airport Munich
A book about rethinking Java EE Patterns
Tweet Follow @AdamBien

Hi,
it seems accessing ordinary (not synchronized) Maps concurrently can lead to several problems (therefore its not allowed). We observed a similar problem in a for loop in the put method using an ordinary HashMap:
java.lang.Thread.State: RUNNABLE
at java.util.HashMap.put(HashMap.java:374)
at java.util.HashMap.putAll(HashMap.java:524)
...
Jan
Posted by Jan Wiemer on October 24, 2012 at 08:26 AM CEST #
It seems that this was fixed in JDK 7: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6425537
Posted by Juraj Martinka on October 29, 2012 at 10:38 AM CET #
Hi,
I agree with Jan.
It happened to me using RichFaces (3.3.3). It was a framework bug (https://issues.jboss.org/browse/RF-7248).
So interesting.
Martin.!
Posted by Martín Dominguez on October 29, 2012 at 04:01 PM CET #