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

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

问题描述

我正在阅读关于Java中的并发性的官方Oracle文档,我想知道 c> c> c>返回的之间的区别可能是什么?

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);

并使用例如

code> ConcurrentHashMap 。我假设在 HashMap 上使用 synchronizeCollection(Collection< T> c)。我知道一个synchronized集合本质上只是一个装饰器为我的 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的迭代器不是线程安全的。

  • 不公开内部锁。 synchronizeCollection。

  • Multiple partitions which can be locked independently. (16 by default)
  • Using concurrent Locks operations for thread safety instead of synchronized.
  • Has thread safe Iterators. synchronizedCollection's iterators are not thread safe.
  • Does not expose the internal locks. synchronizedCollection does.

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

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