Java Hashtable多次访问问题 [英] Java Hashtable many accesses problem

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

问题描述

我正在开发工具,该工具使用Java代码并生成用于获取基本块,循环和方法的执行时间估计的代码.在执行了某些块之后,我们就该开始使用我们的工具了.程序模型存储在下一个表示形式

I'm developing tool, which takes Java code and generate code for getting estimations for execution time of basic blocks, loops and methods. After some block is executed, we put it's time to our tool. The program model is stored in next representation

static Hashtable<String, Hashtable<Integer, Hashtable<String, pair>>> method2Data = new Hashtable<String, Hashtable<Integer, Hashtable<String, pair>>>();

static Hashtable<String, Vector<String>> class2Method = new Hashtable<String, Vector<String>>();

以及方法所花费的时间

 public static void addBlock(int id, double time, String meth, String thread);

但是我有下一个问题.在每次调用 addBlock 时,我们从 method2data 中获取一些信息.因为我们可以有一个类似

But I have next problem. On each call of addBlock, we take something from method2data. Since we could have a code like

for (int i = 0; i < n; i++)
  for (int j = 0; j < n; j++)
    for (int k = 0; k < n; k++) {
      addBlock(0,...);
      addBlock(m,...);
    }

我们经常打电话给 addBlock . 不幸的是,经过一段时间的研究,程序才停止工作.它看起来仍然像进程,但不占用任何CPU.我发现,如果我删除了从 method2data 中获取内容的代码,那么一切正常.因此,我猜想对Hashtable的访问存在一些问题.有什么好主意吗?

we call addBlock a lot of time. Unfortunately, after some constant time of working on our claster, program just stop working. It still looks like process, but doesn't take any cpu. I found, that if I remove code, which take something from method2data, then all is ok. So, I guess, that there is some problem with accesses to Hashtable. Have anybody good ideas?

似乎所有人都感谢我在并发访问时遇到死锁,并且在没有并发事件的情况下可能会耗尽内存.

Thanks to all, it seems, that I have a deadlock in case of concurrent accesses and perhaps could run out of memory when there is no concurrent things.

推荐答案

如果您使用的是Java5或更高版本,则不应使用

If you are using Java5 or above, you shouldn't use Hashtable, but ConcurrentHashMap, which provides much better scalability via lock striping, thus might solve the problem instantly (in case you have a deadlock or starvation issue, which is a possibility based on your - incomplete - description). And on the same line, don't use Vector, but some List implementation instead. Both Hashtable and Vector are old collection implementations which are synchronized, in this Vector's case possibly unnecessarily.

[更新] 正如@finnw正确指出的那样,即使ConcurrentHashMap 似乎可以提供帮助,您也无法确定根本原因是否得到了真正解决.需要进行彻底的分析和测试,以查看您的代码是否实际上是线程安全的.如果没有看到addBlock()中的代码,就无法得出结论. [/更新]

[Update] As @finnw rightly pointed out, even if the ConcurrentHashMap seems to help, you can't be sure that the root cause is really solved. Thorough analysis and testing is required to see whether or not your code is actually thread safe. Without seeing the code in addBlock(), we can't conclude on this. [/Update]

另外,正如其他人指出的那样,对于大型程序而言,将配置文件数据保存在内存中并不是一个好主意,因为这可能会影响您要测量的性能特征,甚至可能会耗尽内存.

Also, as others noted, keeping the profile data in memory is not a good idea for large programs, as this may influence the performance characteristics you are trying to measure, and you may even run out of memory.

这篇关于Java Hashtable多次访问问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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