concurrenthashmap

Usage of HashMap in a multi-threaded environment for regular update

倾然丶 夕夏残阳落幕 提交于 2020-01-04 06:14:15
问题 I have a Java application in which I maintain set of IPs of my other servers in a hash map in memory. The hash map contains mapping between servers instance ids to servers ip address. I also maintain these servers information in a database for persistence. I am trying to solve a simple problem where I just need to cache the servers information in memory for faster access. So I have used hashmap for that. And I need to make sure that the server information in memory are NOT stale and all the

ConcurrentHashmap in JDK8 code explanation

南笙酒味 提交于 2019-12-31 22:40:14
问题 I have been trying to understand the ConcurrentHashMap functions in JDK8, in contrast of how it was in JDK7 (which, in addition to the source code, can be found explained quite well by some nice folks out there such as Richard http://www.burnison.ca/articles/the-concurrency-of-concurrenthashmap). It looks like have been changed quite a bit in JDK8 - e.g. there is no more 'segment' per se, but somehow I got a feeling that the changes are meant to make the code simpler? I'm having some

Java并发源码:ConcurrentHashMap

为君一笑 提交于 2019-12-31 21:28:43
Java并发源码:ConcurrentHashMap 为什么要使用ConcurrentHashMap 不安全的HashMap 效率低下的HashTable ConcurrentHashMap的锁分段技术可以有效提升并发访问率 ConcurrentHashMap的结构 ​ ConcurrentHashMap是由Segment数组结构和HashEntry数组结构组成的。 ​ Jdk1.7 Segment是一种可重入锁(继承了ReentrantLock),HashEntry用来存储key-value数据,一个ConcurrentHashMap包含一个Segment数组。 ​ Jdk1.8 ConcurrentHashMap取消了Segment分段锁,采用CAS和synchronized来保证并发安全。数据结构跟HashMap1.8的结构类似,数组+链表/红黑二叉树。Java 8在链表长度超过一定阈值(8)时将链表(寻址时间复杂度为O(N))转换为红黑树(寻址时间复杂度为O(log(N)))。 ​ Synchronized只是对首节点进行锁定,只要hash不冲突,就不会产生并发。 ​ 下面是一些ConcurrentHashMap的变量。 /** * Table initialization and resizing control. When negative, the * table

combine putIfAbsent and replace with ConcurrentMap

被刻印的时光 ゝ 提交于 2019-12-31 19:30:14
问题 I have a usecase where I have to insert a new value if the key does not exist in the ConcurrentHashMap replace the old value with a new value if the key already exists in the ConcurrentHashMap, where the new value is derived from the old value (not an expensive operation) I've the following code to offer: public void insertOrReplace(String key, String value) { boolean updated = false; do { String oldValue = concurrentMap.get(key); if (oldValue == null) { oldValue = concurrentMap.putIfAbsent

combine putIfAbsent and replace with ConcurrentMap

∥☆過路亽.° 提交于 2019-12-31 19:29:13
问题 I have a usecase where I have to insert a new value if the key does not exist in the ConcurrentHashMap replace the old value with a new value if the key already exists in the ConcurrentHashMap, where the new value is derived from the old value (not an expensive operation) I've the following code to offer: public void insertOrReplace(String key, String value) { boolean updated = false; do { String oldValue = concurrentMap.get(key); if (oldValue == null) { oldValue = concurrentMap.putIfAbsent

Need simple explanation how “lock striping” works with ConcurrentHashMap

一曲冷凌霜 提交于 2019-12-29 03:19:05
问题 According to Java Concurrency in Practice, chapter 11.4.3 says: Lock splitting can sometimes be extended to partition locking on a variablesized set of independent objects, in which case it is called lock striping. For example, the implementation of ConcurrentHashMap uses an array of 16 locks, each of which guards 1/16 of the hash buckets; bucket N is guarded by lock N mod 16. I still have problems to understand and visualize the lock striping and buckets mechanism. Can someone explain this

HashMap并发导致死循环 CurrentHashMap

风格不统一 提交于 2019-12-27 03:49:01
为何出现死循环简要说明 HashMap闭环的详细原因 cocurrentHashMap的底层机制 为何出现死循环简要说明   HashMap是非线程安全的,在并发场景中如果不保持足够的同步,就有可能在执行HashMap.get时进入死循环,将CPU的消耗到100%。   HashMap采用链表解决Hash冲突。因为是链表结构,那么就很容易形成闭合的链路,这样在循环的时候只要有线程对这个HashMap进行get操作就会产生死循环,   单线程情况下,只有一个线程对HashMap的数据结构进行操作,是不可能产生闭合的回路的。   只有在多线程并发的情况下才会出现这种情况,那就是在put操作的时候,如果size>initialCapacity*loadFactor,hash表进行扩容,那么这时候HashMap就会进行rehash操作,随之HashMap的结构就会很大的变化。很有可能就是在两个线程在这个时候同时触发了rehash操作,产生了闭合的回路。   推荐使用currentHashMap 多线程下 [HashMap] 的问题: 1、多线程put操作后,get操作导致 死循环 。 2、多线程 put非NULL元素后,get操作得到NULL值 。 3、多线程 put操作,导致元素丢失 。 HashMap闭环的详细原因 Java的HashMap是非线程安全的,所以在并发下必然出现问题

并发编程—6ConcurrentHashMap1.7 & 1.8

隐身守侯 提交于 2019-12-26 20:41:47
目录 6 ConcurrentHashMap jdk1.7 6.1 预备知识 6.2 jdk1.7原理和实现 6.3 源码 6.3.1 构造方法 6.2.2 get方法 6.2.3 put方法 8.ConcurrentHashMap jdk1.8 8.1与1.7相比的重大变化 8.2 主要数据结构和关键变量 8.3sizeCtl: 8.4 源码 8.4.1 初始化做了什么事? 8.4.2 在get和put操作中,是如何快速定位元素放在哪个位置的? 8.4.3 扩容操作 6 ConcurrentHashMap jdk1.7 hash算法的介绍 构造方法做了什么 get方法做了什么 put方法做了什么 动态扩容逻辑 6.1 预备知识 hash算法: 就是把任意长度的输入,通过散列算法,变换成固定长度的输出,该输出就是散列值。这种转换是一种压缩映射,也就是,散列值的空间通常远小于输入的空间,不同的输入可能会散列成相同的输出,所以不可能从散列值来唯一的确定输入值。简单的说就是一种将任意长度的消息压缩到某一固定长度的消息摘要的函数。在jdk1.7为了使得hash出来的值更加均匀,在concurrentHashMap里面使用了Wang/Jenkins hash算法再做了一次hash。 HashMap和HashTable的缺陷:

ConcurrentHashMap cas操作以及AtomicLongMap类

情到浓时终转凉″ 提交于 2019-12-26 19:50:15
假设有一个需求:实时统计某个网址的访问次数 方案一 :利用concurrentHashMap,进行统计 private Map<String, Long> wordCounts = new ConcurrentHashMap<>(); public long increase(String word) { Long oldValue = wordCounts.get(word); Long newValue = (oldValue == null) ? 1L : oldValue + 1; wordCounts.put(word, newValue); return newValue; } 在多个线程环境increase()的统计是错误的,因为多个线程用相同的word调用时,很可能会覆盖相互的结果,造成记录的次数比实际出现的次数少。concurrentHashMap 能保证的是每一个操作(put,get,delete…)本身是线程安全的,但是increase是先取、再加多个步骤。 对上面increase方法加锁可以解决问题,但是会降低并发量。 方案二 :借助ConcurrentMap接口定义了几个基于 CAS 操作: private ConcurrentMap<String, Long> wordCounts = new ConcurrentHashMap<>(); public

HashMap or ConcurrentHashMap at Java Controllers?

限于喜欢 提交于 2019-12-25 05:18:14
问题 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? 回答1: 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