Hashmap and hashtable in multithreaded environment(多线程环境下的Hashmap和hashtable)
问题描述
我对这两个集合在多线程环境中的行为感到非常困惑.
I am really confused on how these 2 collections behave in multithreaded environment.
哈希表是同步的,这意味着不会有 2 个线程同时更新它的值,对吧?
Hash table is synchronized that means no 2 threads will be updating its value simultaneously right?
推荐答案
看ConcurrentHashMaps 用于线程安全映射.
Look at ConcurrentHashMaps for Thread safe Maps.
它们提供了 HashTable 的所有功能,性能非常接近 HashMap.
They offer all the features of HashTable with a performance very close to a HashMap.
通过不使用地图范围的锁来获得性能,该集合默认维护一个包含 16 个锁的列表,每个锁用于锁定地图的单个存储桶.您甚至可以配置存储桶的数量 :) 调整它可以根据您的数据提高性能.
Performance is gained by instead of using a map wide lock, the collection maintains a list of 16 locks by default, each of which is used to lock a single bucket of the map. You can even configure the number of buckets :) Tweaking this can help performance depending on your data.
Brian Goetz 的 Java Concurrency in Practice 我不能推荐足够多的东西http://jcip.net/
I can't recommend enough Java Concurrency in Practice by Brian Goetz http://jcip.net/
每次阅读时我都会学到一些新东西.
I still learn something new every time I read it.
这篇关于多线程环境下的Hashmap和hashtable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:多线程环境下的Hashmap和hashtable


基础教程推荐
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 从 python 访问 JVM 2022-01-01
- Java Swing计时器未清除 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 多个组件的复杂布局 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- 大摇大摆的枚举 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01