Adam Bien's Weblog

Wednesday Oct 24, 2012

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)!


Special Event: Java 8 with Java EE 7: "More Power with Less Code", 13th October, 2014

A book about rethinking Java EE Patterns

Comments:

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 10: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 11: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 05:01 PM CET #

Hi,

I got a similar error in a Jasper program:

[STUCK] ExecuteThread: '1' for queue: 'weblogic.kernel.Default (self-tuning)'" id=24 idx=0xb0 tid=46 prio=1 alive, native_blocked, daemon
at java/lang/ref/Reference.get(Reference.java:145)[optimized]
at java/util/WeakHashMap.get(WeakHashMap.java:353)
at org/apache/commons/beanutils/MethodUtils.getMatchingAccessibleMethod(MethodUtils.java:530)......

How could I resolved this issue?

Regards,
Cristo Rodríguez

Posted by Cristo Rodriguez on May 03, 2014 at 04:28 AM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed
realworldpatterns.com
...the last 150 posts
...the last 10 comments
Links
License