ConcurrentHashMap 内部如何工作? [英] How does ConcurrentHashMap work internally?

查看:14
本文介绍了ConcurrentHashMap 内部如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读有关 Java 并发的 Oracle 官方文档,我想知道

I was reading the official Oracle documentation about Concurrency in Java and I was wondering what could be the difference between a Collection returned by

public static <T> Collection<T> synchronizedCollection(Collection<T> c);

并使用例如

ConcurrentHashMap.我假设我在 HashMap 上使用了 synchronizedCollection(Collection c).我知道通常同步集合本质上只是我的 HashMap 的装饰器,所以很明显 ConcurrentHashMap 在其内部有一些不同的东西.你有关于这些实施细节的一些信息吗?

ConcurrentHashMap. I'm assuming that I use synchronizedCollection(Collection<T> c) on a HashMap. I know that in general a synchronized collection is essentially just a decorator for my HashMap so it is obvious that a ConcurrentHashMap has something different in its internals. Do you have some information about those implementation details?

我意识到源代码是公开的:ConcurrentHashMap.java

推荐答案

我会阅读 ConcurrentHashMap 的来源 因为它在细节上相当复杂.总之就是

I would read the source of ConcurrentHashMap as it is rather complicated in the detail. In short it has

  • 可以独立锁定的多个分区.(默认为 16)
  • 使用并发锁操作来确保线程安全而不是同步.
  • 具有线程安全迭代器.synchronizedCollection 的迭代器不是线程安全的.
  • 不暴露内部锁.synchronizedCollection 确实如此.

这篇关于ConcurrentHashMap 内部如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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