Java控制器中的HashMap或ConcurrentHashMap? [英] HashMap or ConcurrentHashMap at Java Controllers?

查看:113
本文介绍了Java控制器中的HashMap或ConcurrentHashMap?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如这里所解释的:Java中的并发哈希映射(HashMap)在Java中是并行哈希映射(HashMap)是线程安全的。 Java控制器用于Web请求,可以从Web同时调用。



我的问题是:我应该在Java中使用并发哈希映射而不是哈希映射吗?

解决方案你只需要一个 ConcurrentHashMap ,如果你正在进行并发读取和写入或两个并发写入。如果你在初始化之后永远不改变地图,一个普通的 HashMap 就足够了。

控制器通常不包含任何请求特定的状态(任何这样的状态都应该作为参数传递给方法),如果以这种方式设计控制器,则不需要控制器内的任何同步。


As explained here: ConcurrentHashMap in Java? concurrent hashmap at Java is thread safe. Java controllers are for web requests and can be called simultaneously from web.

My question is that: Should I use concurrent hash map instead of hash map at Java?

解决方案

You only need a ConcurrentHashMap if you will be doing concurrent read along with a write or two concurrent writes. If you never change the map after initialization, a regular HashMap is sufficient.

Controllers should generally not contain any request-specific state (any such state should be passed in to the methods as parameters), and if you design your controllers this way, you shouldn't need any synchronization within your controllers.

这篇关于Java控制器中的HashMap或ConcurrentHashMap?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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