java - putIfAbsent() 不适用于 ConcurrentHashMap

标签 java concurrency hashmap concurrenthashmap

public static void main(String args[]) throws Exception {
    ConcurrentHashMap<byte[], Integer> dps =
         new ConcurrentHashMap<byte[], Integer>();

    System.out.println(dps.putIfAbsent("hi".getBytes(), 1));
    System.out.println(dps.putIfAbsent("hi".getBytes(), 1));
}

打印

null
null

为什么它不在第二行打印 1 ?我已经阅读了 putIfAbsent 的语义,并且应该保证它可以工作。 (注意:这是从大型并发程序中提炼出来的......如您所见,它现在是单线程的。)

最佳答案

putIfAbsent() not working with ConcurrentHashMap

"hi".getBytes() 不是常量数组,因此您将在那里生成两个不同的对象。如果您执行了类似以下操作,您将看到您的 1

byte[] bytes = "hi".getBytes();
System.out.println(dps.putIfAbsent(bytes, 1));
System.out.println(dps.putIfAbsent(bytes, 1));

byte[] 数组上的 hashCode()equals(...) 方法来自 Object 它只查看对象的引用,而不是其内容。

每当您在Map中存储某些内容时,您都需要确保它覆盖hashCode()equals(...)方法,除非您只想比较引用文献。这是 Java 常见问题解答。请参阅这些文档:Java theory and practice: Hashing it out .

正如 @Mauren 在评论中提到的,要使用 byte[]内容,您必须编写一个包装 byte 的小类[] 并提供适当的 hashCode()equals(...) 方法。或者正如 @CostiCiudatu 提到的,您可以使用 SortedMap 并使用 Comparator for byte[] 来查看数组的内容。

顺便说一句,如果String.getBytes()返回一个new byte[],根据您的StringCoding.StringEncoder类进行编码字符集等.

关于java - putIfAbsent() 不适用于 ConcurrentHashMap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19124737/

相关文章:

java - 有了HashMap和Concurrent HashMap,WeakHashMap有什么用?

java - ExtendedTableDataModel 重置方法如何工作?有文档吗?

java - 如何 "fake"有人点击全屏按钮

database - 并发冲突 : the UpdateCommand affected 0 of the expected 1 records

java - 创建 Java 作业池

java - Java 中的 HashMap

java - TFS Java SDK 在 Tomcat 上运行缓慢

Java泛型基础

java - 当一个线程在尝试获取锁后被挂起时,是否总是有上下文切换?

java - 在 Java 中缓存和重用不可变单例对象的最佳方法是什么?