Java Hashtable put方法减缓了我的应用程序 [英] Java Hashtable put method slow down my application

查看:133
本文介绍了Java Hashtable put方法减缓了我的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要这样做:

Dictionary cache;
cache = new Hashtable();
this.getDocument().putProperty("imageCache", cache);

然后我有一个方法:

cache.put(url, picture);

其中picture是Image对象。
我这样创建:

Where picture is an Image object. I create this way :

public Image getSmiley(String smileyName) {
    BufferedImage img = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
    Graphics g = img.getGraphics();
    ImageIcon myicon = new ImageIcon(getClass().getResource("/ola/smileys/" + smileyName + ".png"));
    myicon.paintIcon(null, g, 0, 0);
    return img;
}

我运行了一个分析,当我调用此方法时,我已经看到了把,应用程序放慢了速度。可能是什么原因?

I have run a profiling and I have seen that when I call this method "put", the application slows down incredibly. What could be the reason ?

非常感谢。

最好的问候

推荐答案

我怀疑这可能是因为你使用 URL 类作为密钥类型。

I suspect that this might be due to your using the URL class as the key type.

javadoc for URL.equals(Object)这样说:


如果两个URL对象具有相同的协议,引用等效主机,主机上具有相同的端口号,以及文件的相同文件和片段,则它们是相等的。

Two URL objects are equal if they have the same protocol, reference equivalent hosts, have the same port number on the host, and the same file and fragment of the file.

如果两个主机名都可以解析为相同的IP地址,则认为两台主机是等效的。否则,如果无法解析任何一个主机名,则主机名必须相等而不考虑大小写;或者两个主机名都等于​​null。

Two hosts are considered equivalent if both host names can be resolved into the same IP addresses; else if either host name can't be resolved, the host names must be equal without regard to case; or both host names equal to null.

由于主机比较需要名称解析,因此该操作是阻塞操作。

Since hosts comparison requires name resolution, this operation is a blocking operation.

当您使用 URL 实例作为地图中的键时,每次将一个键与另一个键进行比较时,您可能会触发DNS查找...这可能需要很长时间。

When you use a URL instance as a key in a map, then each time a key is compared to a different one you could be triggering a DNS lookup ... and that could take a long time.

如果这是你的问题,那么你需要将地图的密钥类型更改为字符串 URI ...或其他没有的地图昂贵的等于(对象)方法。

If this is your problem, then you need to change the key type of the map to String or URI ... or something else that doesn't have an expensive equals(Object) method.

这篇关于Java Hashtable put方法减缓了我的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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