gpt4 book ai didi

java - 在这种情况下,ConcurrentHashMap 和 Synchronized Hash Map 哪个更好?

转载 作者:行者123 更新时间:2023-12-01 16:51:38 26 4
gpt4 key购买 nike

我正在编写一个实用程序类,它使用 map 作为缓存存储。现在由于它要在多线程环境中使用。我想出了在进行 put 操作时使用同步 hashmap 或 ConcurrentHashMap(使用 putIfAbsent),我仍然对它是否容易覆盖键值感到困惑(尽管键值在我的情况下是唯一的)并且两者都有优点和缺点。所以我无法决定。可能还有其他一些我可以用于此目的的缓存存储,确实建议,但我更感兴趣的是知道使用 CHM 或 Hashmap 中的哪一个(如果这是唯一的选择)。

程序的注释中是 CHM 用法,我以为我已经使用了 HashMap。

public final class DateTime {

private static final Map<CountryLanguage, Locale> localeMap = new HashMap<>();

/*private static final Map<CountryLanguage, Locale> localeMap = new ConcurrentHashMap<>();*/

public static String getDateTime1(String pattern, LocalDateTime localDateTime, String language,
String country) {
if (language == null || language.isEmpty()) {
throw new NullPointerException("Language cannot be null or empty");
}
CountryLanguage countryLanguage = new CountryLanguage(language, country);
if (!localeMap.containsKey(countryLanguage)) {
synchronized (localeMap) {
// Putting double lock
if (!localeMap.containsKey(countryLanguage)) {
for (Locale locale : Locale.getAvailableLocales()) {
if (locale.getLanguage().equals(language) && locale.getCountry().equals(country)) {
localeMap.put(new CountryLanguage(language, country), locale);
}
}
}
}
}
/*for (Locale locale : Locale.getAvailableLocales()) {
if (locale.getLanguage().equals(language) && locale.getCountry().equals(country)) {
localeMap.putIfAbsent(new CountryLanguage(language, country), locale);
}
}*/
Locale locale = localeMap.get(countryLanguage);
if (locale == null) {
locale = Locale.ROOT;
}
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern, locale);
return localDateTime.format(dateTimeFormatter);
}

注意:

  • 我有一个内部类CountryLanguage,其中包含字符串国家、字符串语言作为成员变量,并且 hashCode 和 equals 方法都已被重写。

编辑1:我没有使整个 map 同步,我只是在放置操作时在 map 上使用同步。我正在使用双重检查来确保不存在两个键值

最佳答案

Synchronized HashMap:

  1. Each method is synchronized using an object level lock. SO the get and put methods on synchMap acquire a lock.
  2. Locking the entire collection is a performance overhead. While one thread holds on to the lock, no other thread can use the collection.

ConcurrentHashMap: (was introduced in JDK 5)

  1. There is no locking at the object level,The locking is at a much finer granularity. For a ConcurrentHashMap, the locks may be at a hashmap bucket level.
  2. The effect of lower level locking is that you can have concurrent readers and writers which is not possible for synchronized collections. This leads to much more scalability.
  3. ConcurrentHashMap does not throw a ConcurrentModificationException if one thread tries to modify it while another is iterating over it.

所以,我推荐你ConcurrentHashMap,它不会一直阻塞所有的“cahce”。

如果您想了解更多信息HashMap vs ConcurrentHashMap

编辑:

关于此的其他一些帖子: Is ConcurrentHashMap totally safe?

关于java - 在这种情况下,ConcurrentHashMap 和 Synchronized Hash Map 哪个更好?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39011002/

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