gpt4 book ai didi

Java Hashtable put 方法减慢了我的应用程序的速度

转载 作者:行者123 更新时间:2023-12-02 04:40:55 25 4
gpt4 key购买 nike

我需要做:

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

然后我有一个方法:

cache.put(url, picture);

其中图片是一个图像对象。我这样创建:

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;
}

我已经运行了分析,并且发现当我调用此方法“put”时,应用程序的速度明显减慢。可能是什么原因?

非常感谢。

最诚挚的问候

最佳答案

我怀疑这可能是由于您使用 URL 类作为键类型造成的。

javadoc对于 URL.equals(Object) 来说:

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.

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 实例作为 map 中的键时,每次将一个键与另一个键进行比较时,您可能会触发 DNS 查找......这可能需要很长时间时间。

<小时/>

如果这是您的问题,那么您需要将映射的键类型更改为 StringURI ...或其他没有昂贵的 equals(Object) 方法。

关于Java Hashtable put 方法减慢了我的应用程序的速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8426139/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com