简体   繁体   中英

Java ConcurrentHashMap.keySet() hangs

I am observing a strange behavior on a Linux box. The code works fine on Win 7. The offending code hangs on xmlToJavaMap.keySet(). Neither of the two log statements are logged!!! No deadlock found in heap dump.

    ConcurrentHashMap<String,String> xmlToJavaMap = ApplicationContext.getBean("map");
    logger.info("before for loop");
    for (String key : xmlToJavaMap.keySet()) {
        logger.info("key: " + key);
        ...
    }   
    logger.info("map processed.");  

Platform: java version "1.7.0_11" Java(TM) SE Runtime Environment (build 1.7.0_11-b21) Java HotSpot(TM) 64-Bit Server VM (build 23.6-b04, mixed mode) Red Hat 4.4.7

Use jps -v to watch your java process. Then use jstack to watch your stack of threads. That could help you find the solution.

Here is what was actually happening :) xmlToJavaMap.keySet() was actually failing with NoSuchMethod and the thread was terminating. The error stack trace was getting logged in a different log file which was causing the confusion. Once the error was addressed , everything back to normal.

I found the same case, hang at keySet() method. actually the thread exit by NoSuchMethodError error. solution is that just declare

ConcurrentHashMap<String,String> xmlToJavaMap = ApplicationContext.getBean("map");

to

Map<String,String> xmlToJavaMap = ApplicationContext.getBean("map");

or

make sure code and dependency jar is compiled by same jdk version.

the root reason is that "Undefined reference: .. ConcurrentHashMap.keySet()" when building in Java 8 , cause by: in version 7 and 8, keySet() method is not the same return , this error maybe is not in your project's log or console, but in heap. you can dump heap to find java.lang.NoSuchMethodError related to keySet()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM