concurrenthashmap

Data inconsistency using ConcurrentHashMap

人盡茶涼 提交于 2019-12-12 10:16:47
问题 The count changes for every run for the same set of files. The following code is still not data consistent. How to make thread safe? Simple word count code. package ConcurrentHashMapDemo; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; class FileReaderTask implements Runnable { private String filePath; private String fileName; private ConcurrentMap

Modifying values in ConcurrentHashMap

送分小仙女□ 提交于 2019-12-12 10:04:19
问题 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

Disadvantage of increasing number of partition in Java ConcurrentHashMap?

时光怂恿深爱的人放手 提交于 2019-12-12 09:37:36
问题 Java ConcurrentHashMap maintains partitions internally. Each partition can have locking separately. There can be scenarios where all the keys accessed by multiple thread fall into the same partition and partitions may not be helpful. Increasing the number of partitions further should improve the concurrency. Why does Java provides default value for partition count as 16 instead of very high value? What is the performance overhear with large number of partitions in the Map? 回答1: Why does Java

How to atomically update the value of ConcurrentMap in multithreaded application?

陌路散爱 提交于 2019-12-12 04:22:47
问题 I have a ConcurrentMap which I need to populate from multithread application. My map is shown below: private final ConcurrentMap<String, AtomicLongMap<String>> deviceErrorHolder = Maps.newConcurrentMap(); Below is my method which is called from multithreaded application at very fast rate so I need to make sure it is fast. public void addDeviceErrorStats(String deviceName, String errorName) { AtomicLongMap<String> errorMap = deviceErrorHolder.get(deviceName); if (errorMap == null) { errorMap =

Efficiently removing an element added to a ConcurrentQueue

荒凉一梦 提交于 2019-12-12 03:22:07
问题 In principle it is easy to remove an element from ConcurrentLinkedQueue or similar implementation. For example, the Iterator for that class supports efficient O(1) removal of the current element: public void remove() { Node<E> l = lastRet; if (l == null) throw new IllegalStateException(); // rely on a future traversal to relink. l.item = null; lastRet = null; } I want to add an element to the queue with add() , and then later delete that exact element from the queue. The only options I can

并发容器的原理,七大并发容器详解、及使用场景

我是研究僧i 提交于 2019-12-11 23:47:05
并发容器的由来 在Java并发编程中,经常听到Java集合类,同步容器、并发容器,那么他们有哪些具体分类,以及各自之间的区别和优劣呢? 只有把这些梳理清楚了,你才能真正掌握在高并发的环境下,正确使用好并发容器,我们先从Java集合类,同步容器谈起。 1.什么是同步容器 Java的集合容器框架中,主要有四大类别:List、Set、Queue、Map,大家熟知的这些集合类ArrayList、LinkedList、HashMap这些容器都是非线程安全的。 如果有多个线程并发地访问这些容器时,就会出现问题。因此,在编写程序时,在多线程环境下必须要求程序员手动地在任何访问到这些容器的地方进行同步处理,这样导致在使用这些容器的时候非常地不方便。 所以,Java先提供了同步容器供用户使用。 同步容器可以简单地理解为通过synchronized来实现同步的容器 ,比如Vector、Hashtable以及SynchronizedList等容器。 2.同步容器,主要的分类: Vector Stack HashTable Collections.synchronized方法生成 同步容器面临的问题 可以通过查看Vector,Hashtable等这些同步容器的实现代码,可以看到这些容器实现线程安全的方式就是将它们的状态封装起来, 并在需要同步的方法上加上关键字synchronized。

并发容器的原理,七大并发容器详解、及使用场景

夙愿已清 提交于 2019-12-11 22:40:49
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 并发容器的由来 在Java并发编程中,经常听到Java集合类,同步容器、并发容器,那么他们有哪些具体分类,以及各自之间的区别和优劣呢? 只有把这些梳理清楚了,你才能真正掌握在高并发的环境下,正确使用好并发容器,我们先从Java集合类,同步容器谈起。 1.什么是同步容器 Java的集合容器框架中,主要有四大类别:List、Set、Queue、Map,大家熟知的这些集合类ArrayList、LinkedList、HashMap这些容器都是非线程安全的。 如果有多个线程并发地访问这些容器时,就会出现问题。因此,在编写程序时,在多线程环境下必须要求程序员手动地在任何访问到这些容器的地方进行同步处理,这样导致在使用这些容器的时候非常地不方便。 所以,Java先提供了同步容器供用户使用。 同步容器可以简单地理解为通过synchronized来实现同步的容器 ,比如Vector、Hashtable以及SynchronizedList等容器。 2.同步容器,主要的分类: Vector Stack HashTable Collections.synchronized方法生成 同步容器面临的问题 可以通过查看Vector,Hashtable等这些同步容器的实现代码,可以看到这些容器实现线程安全的方式就是将它们的状态封装起来,

With double-checked locking, does a put to a volatile ConcurrentHashMap have happens-before guarantee?

孤人 提交于 2019-12-11 04:24:03
问题 So far, I have used double-checked locking as follows: class Example { static Object o; volatile static boolean setupDone; private Example() { /* private constructor */ } getInstance() { if(!setupDone) { synchronized(Example.class) { if(/*still*/ !setupDone) { o = new String("typically a more complicated operation"); setupDone = true; } } } return o; } }// end of class Now, because we have groups of threads that all share this class, we changed the boolean to a ConcurrentHashMap as follows:

Updating both a ConcurrentHashMap and an AtomicInteger safely

丶灬走出姿态 提交于 2019-12-11 02:15:03
问题 I have to store words and their corresponding integer indices in a hash map. The hash map will be updated concurrently. For example: lets say the wordList is {a,b,c,a,d,e,a,d,e,b} The the hash map will contain the following key-value pairs a:1 b:2 c:3 d:4 e:5 The code for this is as follows: public class Dictionary { private ConcurrentMap<String, Integer> wordToIndex; private AtomicInteger maxIndex; public Dictionary( int startFrom ) { wordToIndex = new ConcurrentHashMap<String, Integer>();

Scala: How to test the concurrency of a mutable.Set

我怕爱的太早我们不能终老 提交于 2019-12-11 00:34:02
问题 In Scala, both concurrent and non-concurrent Sets have exactly the same type: // A regular Set in Scala, not concurrent. val regularSet: mutable.Set[Int] = mutable.Set[Int]() // A concurrent set. It has the same type as a regular set, but underneath, it is actually concurrent. In my opinion, this is a flaw in the type system for Scala collections val concurrentSet: mutable.Set[Int] = java.util.concurrent.ConcurrentHashMap.newKeySet[Int]().asScala I'd like a way to actually test whether a set