简体   繁体   English

如何确保写入发生在同一存储桶和同一键上的不同线程同时读取并发哈希映射之前?

[英]How can I make sure that write happens before read in concurrenthashmap at the same time by different thread at same bucket and on a same key?

I want to make sure that while reading and writing at the same bucket and key, the fetched value should be updated, that is it should be fetched after the write operation to it.我想确保在同一个桶和键读取和写入时,应该更新获取的值,即应该在对其进行写入操作后获取它。 how can I achieve that?我怎样才能做到这一点?

What I want is if I update the value for one key and for the same key if I fetch the value with different thread at the same time then I should get the updated value not the previous one, this is what I want to achieve.我想要的是如果我更新一个键的值,如果我同时用不同的线程获取值,那么我应该得到更新的值而不是前一个,这就是我想要实现的。

I can think of using wait/notify, join to use here before any operation but I am not sure about it.我可以考虑使用等待/通知,在任何操作之前加入此处使用,但我不确定。

We all know how concurrent hashmap works if not, here: ConcurrentHashmap simultaneous write and get operations我们都知道如果没有并发 hashmap 是如何工作的,这里: ConcurrentHashmap 同时写入和获取操作

According to this article Concurrent hasmap already has that feature, please refer the below para and link Read-Write Operation:- Two threads can read and write data on different segments at the same time without blocking each other.根据这篇文章并发hasmap已经有这个特性,请参考下面的para和链接读写操作:-两个线程可以同时在不同的段上读写数据,而不会互相阻塞。 In general, Retrieval operations do not block, so may overlap with write (put/remove) operations.通常,检索操作不会阻塞,因此可能与写入(放置/删除)操作重叠。 Latest updated value will be returned by get operation which is most recently updated value by write operation (including put/remove)最新更新的值将通过 get 操作返回,这是最近通过写入操作更新的值(包括 put/remove)

https://medium.com/@itsromiljain/curious-case-of-concurrenthashmap-90249632d335 https://medium.com/@itsromiljain/curious-case-of-concurrenthashmap-90249632d335

PS: I have not tried it, please update me if the above article(curious case) mentioned wrongly. PS:我没试过,如果上面的文章(奇案)有错误的地方请更新我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果2个不同的写入和读取线程永远不会同时存在,我是否需要使用volatile - Do I need to use volatile, if 2 different write and read thread will never alive at the same time 如何使多个按键绑定同时工作? - How can i make multiple Key Bindings work at the same time? 线程不能同时在同一个套接字上读写流? - Thread cannot read and write streams on the same socket at the same time? ConcurrentHashMap-添加具有相同Key的项目 - ConcurrentHashMap - adding items with the same Key 线程1正在从段n上的ConcurrentHashMap中读取数据,线程2正在相同的sagment中更新值,这是否会使线程1变脏? - Thread 1 is reading from ConcurrentHashMap on segment n and Thread 2 is updating value in same sagment.Won't it make thread 1 read dirty? 我可以使用两个不同的线程在同一个套接字上读写吗? - Can I read and write on the same socket using two different threads? 确保在同一个(EDT)事件调度线程中 - Make sure in same (EDT) event dispatching thread 在另一个线程同时记录日志的同时,如何安全地读取Log4j日志? - How can I safely read Log4j logs while another thread is logging at the same time? 当读取和写入请求来自同一段时,ConcurrentHashMap 如何工作? - How does ConcurrentHashMap work when read and write request come for the same segment? 如何同时写和读文件 - How to write and read to file reactively in the same time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM