concurrenthashmap

并发容器之ConcurrentHashMap(JDK 1.8版本)

本秂侑毒 提交于 2019-12-06 05:27:57
1.ConcurrentHashmap简介 在使用HashMap时在多线程情况下扩容会出现CPU接近100%的情况,因为hashmap并不是线程安全的,通常我们可以使用在java体系中古老的hashtable类,该类基本上所有的方法都采用synchronized进行线程安全的控制,可想而知,在高并发的情况下,每次只有一个线程能够获取对象监视器锁,这样的并发性能的确不令人满意。另外一种方式通过Collections的 Map<K,V> synchronizedMap(Map<K,V> m) 将hashmap包装成一个线程安全的map。比如SynchronzedMap的put方法源码为: public V put(K key, V value) { synchronized (mutex) {return m.put(key, value);} } 实际上SynchronizedMap实现依然是采用synchronized独占式锁进行线程安全的并发控制的。同样,这种方案的性能也是令人不太满意的。针对这种境况,Doug Lea大师不遗余力的为我们创造了一些线程安全的并发容器,让每一个java开发人员倍感幸福。相对于hashmap来说,ConcurrentHashMap就是线程安全的map,其中利用了锁分段的思想提高了并发度。 ConcurrentHashMap在JDK1

Pattern for Java ConcurrentHashMap of Sets

不想你离开。 提交于 2019-12-06 05:11:45
问题 A data structure that I use commonly in multi-threaded applications is a ConcurrentHashMap where I want to save a group of items that all share the same key. The problem occurs when installing the first item for a particular key value. The pattern that I have been using is: final ConcurrentMap<KEYTYPE, Set<VALUETYPE>> hashMap = new ConcurrentHashMap<KEYTYPE, Set<VALUETYPE>>(); // ... Set<VALUETYPE> newSet = new HashSet<VALUETYPE>(); final Set<VALUETYPE> set = hashMap.putIfAbsent(key, newSet)

Modifying values in ConcurrentHashMap

不打扰是莪最后的温柔 提交于 2019-12-06 04:29:06
In ConcurrentHashMap there is concept of segmentation. What it means if two threads are trying to access ConcurrentHashMap they it gets divided in two blocks and default size of blocks is 16. Now suppose in a scenario where ConcurrentHashMap has only two elements and two different threads comes and thread1 tries to modify first value and thread2 tries to modify second value. In this case whether ConcurrentHashMap will go for segmentation? Now in a different scenario both the threads try to modify same value how ConcurrentHashMap will handle this situation? By using locking mechanism or is

ConcurrentHashMap, which concurrent features improved in JDK8

余生颓废 提交于 2019-12-05 14:47:51
Can any concurrent expert explain in ConcurrentHashMap, which concurrent features improved comparing with which in previous JDKs Well, the ConcurrentHashMap has been entirely rewritten. Before Java 8, each ConcurrentHashMap had a “concurrency level” which was fixed at construction time. For compatibility reasons, there is still a constructor accepting such a level though not using it in the original way. The map was split into as many segments, as its concurrency level, each of them having its own lock, so in theory, there could be up to concurrency level concurrent updates, if they all

JUC-3-ConcurrentHashMap

江枫思渺然 提交于 2019-12-05 11:46:14
ConcurrentHashMap 锁分段机制 JDK1.8 ConcurrentHashMap使用的是分段锁技术,ConcurrentHashMap 是一个 Segment 数组,Segment 通过继承 ReentrantLock 来进行加锁,所以每次需要加锁的操作锁住的是一个 segment,这样只要保证每个 Segment 是线程安全的,也就实现了全局的线程安全。 来源: https://www.cnblogs.com/wf-zhang/p/11923163.html

Is this code a thread-safe one?

自古美人都是妖i 提交于 2019-12-05 08:00:26
I want to process a flow of client requests. Each request has its special type. First I need to initialize some data for that type, and after this I can start processing the requests. When the client type comes for the first time, I just initialize the corresponding data. After this all the following requests of that type are processed using that data. I need to do this in a thread-safe manner. Here is a code I have written. Is it thread-safe? public class Test { private static Map<Integer, Object> clientTypesInitiated = new ConcurrentHashMap<Integer, Object>(); /* to process client request we

Dubbo 和 JDK 版本不匹配

偶尔善良 提交于 2019-12-05 04:30:23
Maven项目,使用Dubbo 2.8.4,启动报错: POM.XML依赖如下: <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.8.4</version> </dependency> Eclipse报错如下: Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.qhgrain.api.iUnitService': Instantiation of bean failed; nested exception is java.lang.ExceptionInInitializerError at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1101) at org.springframework.beans.factory.support

Java ConcurrentHashMap not thread safe.. wth?

泪湿孤枕 提交于 2019-12-05 03:16:44
I was using HashMap before like public Map<SocketChannel, UserProfile> clients = new HashMap<SocketChannel, UserProfile>(); now I've switched to ConcurrentHashMap to avoid synchronized blocks and now i'm experiencing problems my server is heavily loaded with 200-400 concurrent clients every second which is expected to grow over time. which now looks like this public ConcurrentHashMap<SocketChannel, UserProfile> clients = new ConcurrentHashMap<SocketChannel, UserProfile>(); My server design works like this. I have a worker thread(s) for processing huge amounts of packets. Each packet is checked

Java中的锁

别来无恙 提交于 2019-12-05 02:55:31
一、乐观锁   乐观锁是一种乐观思想,即认为读多写少,遇到并发写的可能性低,每次去拿数据时都认为别人不会修改,所以不会上锁,但是在更新时会判断一下在此期间别人有没有去更新这个数据,采取在写时先读出当前版本号,然后加锁操作(比较跟上一次的版本号,如果一样则更新),如果失败则要重复读-比较-写操作。   Java中的乐观锁基本是通过CAS操作实现的,CAS是一种更新的原子操作,比较当前值跟传入值是否一样,一样则更新,否则失败。 二、悲观锁   悲观锁就是悲观思想,即认为写多,遇到并发写的可能性高,每次去拿数据时都认为被人会修改,所以每次在读写数据时都会上锁。这样别人想读写这个数据就会block知道拿到锁。Java中的悲观所就是Synchronized,AQS框架下的锁则是先尝试CAS乐观锁去获取锁,若获取不到时,才会转换为悲观锁,如RetreenLock。 三、Synchronized同步锁 可以把任意一个非null的对象当作锁,它属于独占式的悲观锁,同时属于可重入锁。 作用范围:   1)作用于方法时,锁住的是对象的实例(this);   2)作用于静态方法时,锁住的是Class实例,又因为Class的相关数据存储在永久代PermGen(JDK1.8则是metaspace),永久代是全局共享的,因此静态方法相当于类的一个全局锁,会锁所有调用该方法的线程。   3

Java Remove Specific Item From ConcurrentHashMap

柔情痞子 提交于 2019-12-05 01:22:47
Is using the remove() method okay? I've read an article that synchronization hasn't been added to the remove method. How do I properly remove a specific item from a ConcurrentHashMap? Example Code: ConcurrentHashMap<String,Integer> storage = new ConcurrentHashMap<String,Integer>(); storage.put("First", 1); storage.put("Second", 2); storage.put("Third",3); //Is this the proper way of removing a specific item from a tread-safe collection? storage.remove("First"); for (Entry<String, Integer> entry : storage.entrySet()) { String key = entry.getKey(); Object value = entry.getValue(); // ... System