Java多线程程序挂起访问地图 [英] Java multi-thread program hang on access to a map

查看:162
本文介绍了Java多线程程序挂起访问地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多线程程序,在运行一段时间后会挂起。杀死它后的堆栈显示每个线程都挂在concurrentMap的get方法上。
我想知道可能导致这个问题的原因是什么?
我想知道:
1:使用concurrentMap时是否可能出现死锁?我只做简单的get / put ....
2:基本上什么可能导致多线程程序挂起?糟糕的程序是否有可能耗尽内存?
谢谢!

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:

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

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

和程序片段:

    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_是AtomicInteger

featureCount_ is an AtomicInteger

应该是非常简单的代码.....

Should be really simple code.....

推荐答案

这取决于什么样的 ConcurrentMap 你正在使用,但一般来说,这些结构可以 livelock 。也就是说,尝试进行修改的线程会花费他们所有的时间来决定谁先行,然后再也不会做出改变。

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.

我猜这个你的意思是你实际上使用的是 ConcurrentHashMap 。在这种情况下,请确保已设置 concurrencyLevel 正确(它应该与进行并发修改的线程数相当匹配,错误偏高)。

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

在另一条调查路径上,您使用的是JRockit吗?它的优化可能会导致它们不应发生的死锁。

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

这篇关于Java多线程程序挂起访问地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆