ConcurrentHashmap同时进行写和获取操作 [英] ConcurrentHashmap simultaneous write and get operations

查看:112
本文介绍了ConcurrentHashmap同时进行写和获取操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对ConcurrentHashMaps有疑问。可以说我有2个线程。
线程A尝试从共享的ConcurrentHashMap获取对象。
线程B清除共享映射。
如果线程A和线程B同时访问共享资源,会发生什么情况。我在文档和网络上进行了搜索,找不到明确的答案,也尝试自己做,但无济于事。

I have a question about ConcurrentHashMaps. Lets say I have 2 threads. Thread A tries to get an object from a shared ConcurrentHashMap. Thread B clears the shared map. What happens if Thread A and Thread B access the shared resource simultaneously, at the very same time. I searched the documentation and the web and can't find a definitive answer, also tried to do it myself but to no avail.

推荐答案

ConcurrentHashMap根据并发级别分为不同的。因此,不同的线程可以在Java中同时访问不同的段。

ConcurrentHashMap is divided into different segments based on concurrency level. So different threads can access different segments concurrently in java.


线程可以读取由其他
线程锁定的ConcurrentHashMap的段。 java?

Can threads read the segment of ConcurrentHashMap locked by some other thread in java?

是。当线程锁定一个段进行更新时,它不会阻止它进行检索(通过get方法完成),因此其他线程可以读取该段(通过get方法),但是在锁定之前它将能够读取数据。

Yes. When thread locks one segment for updation it does not block it for retrieval (done by get method) hence some other thread can read the segment (by get method), but it will be able to read the data before locking.

对于诸如 putAll 之类的操作,并发检索可能只反映了某些条目的删除。
对于 clear 之类的操作,并发检索可能只反映了某些条目的删除。

For operations such as putAll concurrent retrievals may reflect removal of only some entries. For operations such as clear concurrent retrievals may reflect removal of only some entries.

这篇关于ConcurrentHashmap同时进行写和获取操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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