Java中ConcurrentHashMap的节点HashEntry 中元素为什么要定义成final的?
大家讲道理
大家讲道理 2017-04-17 16:13:32
0
1
821
static final class HashEntry<K,V> {
    final K key;
    final int hash;
    volatile V value;
    final HashEntry<K,V> next;
}

这样导致在remove节点的时候,需要将remove节点的前面节点全部复制一遍,真的好吗?

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(1)
巴扎黑

When a hash conflict occurs, a linked list will be used to resolve the conflict, but the probability of a hash conflict is very small, and the map will automatically expand to reduce conflicts.

For using final, the Java compiler will ensure that the final field must have been written before reading, that is, ensuring that writing occurs before reading, to avoid writing after reading and reading unexpected values ​​under multi-thread concurrency conditions.

Portal:java memory model final

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template