[英]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.