需要简单的解释如何“锁定条纹”使用ConcurrentHashMap [英] Need simple explanation how "lock striping" works with ConcurrentHashMap

查看:216
本文介绍了需要简单的解释如何“锁定条纹”使用ConcurrentHashMap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据Java Concurrency in Practice,第11.4.3章说:

According to Java Concurrency in Practice, chapter 11.4.3 says:


锁分裂有时可以扩展到分区
锁定一组变量的独立对象,在这种情况下
它被称为锁定条带化。例如,
的实现ConcurrentHashMap使用16个锁的数组,每个锁保护散列桶的1/16
; bucket N被N mod 16保护。

Lock splitting can sometimes be extended to partition locking on a variablesized set of independent objects, in which case it is called lock striping. For example, the implementation of ConcurrentHashMap uses an array of 16 locks, each of which guards 1/16 of the hash buckets; bucket N is guarded by lock N mod 16.

我仍然有问题来理解和可视化锁条带和桶机制。
有人可以用很好的理解词解释这一点:)

I still have problems to understand and visualize the lock striping and buckets mechanism. Can someone explain this with good understanding words :)

提前感谢。

推荐答案

哈希映射建立在数组上,其中哈希函数将对象映射到底层数组中的元素。让我们说底层数组有1024个元素 - ConcurrentHashMap实际上将它转换为16个不同的64个元素的子数组,例如。 {0,63},{64,127}等。每个子数组都有自己的锁,因此修改{0,63}子数组不会影响{64,127}子数组 - 可以写入第一个子数组,而另一个线程写入第二个子数组。

The hash map is built on an array, where the hash function maps an object to an element in the underlying array. Let's say the underlying array has 1024 elements - ConcurrentHashMap actually turns this into 16 different sub-arrays of 64 elements, e.g. {0, 63}, {64, 127}, etc. Each sub-array has its own lock, so modifying the {0, 63} sub-array doesn't impact the {64, 127} sub-array - one thread can write to the first sub-array while another thread writes to the second sub-array.

这篇关于需要简单的解释如何“锁定条纹”使用ConcurrentHashMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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