多线程环境下的Hashmap和hashtable

Hashmap and hashtable in multithreaded environment(多线程环境下的Hashmap和hashtable)

本文介绍了多线程环境下的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

基础教程推荐