1

I have a multithreading program which will hang after running a while. The stack after killing it shows that every single thread is hanging on the get method of a concurrentMap. I wonder what may cause this problem? I am wondering: 1: Is it possible to have deadlock when using concurrentMap? I am only doing simple get/put.... 2: Basically what may cause a multi-thread program to hang? Is it possible that the poor program just runs out of memory? Thank you!

Thank you all for the help. Just to make the problem specific:

Every worker thread is hang in the following state:

Classifier0" prio=10 tid=0x00007fda04420800 nid=0x16a2 runnable [0x00000000414ea000] java.lang.Thread.State: RUNNABLE at java.util.concurrent.ConcurrentHashMap.get()

And the program snippet:

    for (String feature : features) {
        Integer ti = this.descMap_.get(feature);
        if (ti == null) {
            ti = this.featureCount_.incrementAndGet();
            this.descMap_.put(feature, ti);
            this.featureMap_.put(ti, feature);
        }

        ans.add(new Entry<Integer, Value>(ti, tval));
    }

featureCount_ is an AtomicInteger

Should be really simple code.....

bwei
  • 11
  • 2
  • 3
    It would help if you would post your code and the stack trace upon exit (hung process). – JJ. Aug 25 '11 at 16:55
  • ConccurentHashMap.get will never block (in theory with Java 6) so there should be no deadlock. What line is it stuck on? – John Vint Aug 25 '11 at 16:55
  • If you ran out of memory, you should throw an out of memory exception, not just hang. – Jay Aug 25 '11 at 17:02
  • @Jay bwei would have no control over that. The JVM would throw an OOM error. – John Vint Aug 25 '11 at 17:05
  • @bwei I am assuming your are using the standard JDK and not OpenJDK? There should be no reason why the CHM.get would go into a livelock unless you are changing the hashcode fields or you run out of memory. Did you see if you maxed memory when this happened? – John Vint Aug 25 '11 at 19:43
  • Oops, after reading John V's comment, I see that my post was poorly worded. I didn't mean "you should" in the sense of "I recommed that you do this". I meant "this is what you would expect to happen", like "If you keep your tires properly inflated, you should get better gas mileage." If you run out of memory, the JVM will throw an out of memory error. My point was that this is apparently NOT what happened, as his program hung rather than throwing the exception. – Jay Aug 25 '11 at 20:50

1 Answers1

1

It depends on what kind of ConcurrentMap you are using, but in general, these structures can livelock. That is, the threads trying to make modifications spend all of their time deciding who gets to go first, and never get around to making a change.

I'm guessing that you mean you are actually using a ConcurrentHashMap. In that case, make sure that you have set the concurrencyLevel correctly (it should match the number of threads making concurrent modifications fairly closely, erring on the high side).

On an alternative path of investigation, are you using JRockit? Its "optimizations" can cause deadlocks in places where they should not occur.

erickson
  • 265,237
  • 58
  • 395
  • 493