多线程环境中的散列表和散列表 [英] Hashmap and hashtable in multithreaded environment

查看:130
本文介绍了多线程环境中的散列表和散列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这两个集合在多线程环境中的表现感到困惑。

哈希表是同步的,意味着没有2个线程会同时更新它的值吗?

解决方案

查看 ConcurrentHashMap 用于线程安全映射。



它们提供了HashTable的所有功能,其性能非常接近HashMap。



通过不使用地图范围锁来获得性能,该集合默认维护16个锁的列表,每个锁都用于锁定一个桶地图。你甚至可以配置桶的数量:)根据你的数据调整它可以帮助提高性能。



我不能推荐Brian Goetz的b实践中足够的Java Concurrency
http://jcip.net/



I每次读它时仍然会学到新的东西。


I am really confused on how these 2 collections behave in multithreaded environment.

Hash table is synchronized that means no 2 threads will be updating its value simultaneously right?

解决方案

Look at ConcurrentHashMaps for Thread safe Maps.

They offer all the features of HashTable with a performance very close to a HashMap.

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.

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.

这篇关于多线程环境中的散列表和散列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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