concurrenthashmap

Concurrent Cache in Java

大城市里の小女人 提交于 2019-12-24 15:27:55
问题 Im looking for an explanation to the following code from Brian Goetz's concurrency book. public V compute(final A arg) throws InterruptedException { while (true) { Future<V> f = cache.get(arg); if (f == null) { Callable<V> eval = new Callable<V>() { public V call() throws InterruptedException { return c.compute(arg); } }; FutureTask<V> ft = new FutureTask<V>(eval); f = cache.putIfAbsent(arg, ft); if (f == null) { f = ft; ft.run(); } } try { return f.get(); } catch (CancellationException e) {

Exclusively Locking ConcurrentHashMap

三世轮回 提交于 2019-12-24 12:34:29
问题 I know that it is not possible to lock a ConcurrentHashMap for exclusive access. However, I cannot find why. Is it because the "Segments" which constitue CHM aren't exposed by the api? Presumably if they were, the client code could perform a "hand-over-hand" locking? Cheers 回答1: I know that it is not possible to lock a ConcurrentHashMap for exclusive access. However, I cannot find why. Simple - because it is not true. How about single instance per thread? How about synchronized methods or

java

廉价感情. 提交于 2019-12-24 00:45:54
第十六章 Java final类不能继承、重写,final方法不能重写,final属性不能变 16.1 JVM 组成 JVM内存大致分为五个区域:方法区、虚拟机栈、本地方法栈、堆、程序计数器 **程序计数器:**记录的是正在执行的虚拟机字节码指令的地址,通过改变程序计数器,java程序才能按顺序、循环、跳转等流程执行各个方法。该区域是所有区域中唯一没有定义内存溢出错误的区域。 **虚拟机栈:**java为每个方法保存状态信息的区域,这里存放的是每个方法中的局部变量、方法出口、动态链接等,著名的栈溢出错误就是在这里发生。 **本地方法栈:**java可以执行非java函数,这些函数的状态信息就保存在这个区域,因此这个区域也有可能发生栈溢出。 **堆:**一块线程共享的存放对象实例和数组的内存区域,线程安全问题的根本原因,也是整个内存区域中最大的一块。 **方法区:**存储已被加载的类信息、常量、静态变量等,著名的常量池就位于这里。 类加载机制 当程序主动使用某个类时,如果该类还未被加载到内存中,则JVM会通过 加载、连接、初始化 3个步骤来对该类进行初始化。如果没有意外,JVM将会连续完成3个步骤,所以有时也把这个3个步骤统称为类加载或类初始化。 **加载:**将类的class文件读入到内存,创建一个类对象的过程,加载的方法有三种: new的方式加载、调用类反射的方法加载

Java ConcurrentHashMap atomic get if present

久未见 提交于 2019-12-23 13:02:22
问题 How do you perform a safe get if present operation on a Concurrent Hash Map? (same thing like putIfAbsent) Bad example, not very thread safe (check then act situation): ConcurrentMap<String, SomeObject> concMap = new ... //... many putIfAbsent and remove operations public boolean setOption(String id, Object option){ SomeObject obj = concMap.get(id); if (obj != null){ //what if this key has been removed from the map? obj.setOption(option); return true; } // in the meantime a putIfAbsent may

Concurrent Map with fixed size

两盒软妹~` 提交于 2019-12-23 07:00:54
问题 I need a map with the following requirements : It should be highly concurrent. The put() , get() and remove() methods may be called by multiple threads simultaneously. It should be of fixed size. If the size of the HashMap reaches to the max value (e.g. 10000), addition of a new entry to the map should not be allowed. It CAN'T be LRU cache where the oldest entry gets removed on reaching maximum size. ConcurrentHashMap may satisfy #1. But, not sure how #2 can be implemented on top of

JVM HeapDump: The memory is accumulated in one instance of "java.util.concurrent.ConcurrentHashMap$Segment

醉酒当歌 提交于 2019-12-23 02:17:01
问题 I have trouble with the JVM-Heap. We operate a website with Apache HTTP Server and an Apache Tomcat Application Server. All *.jsp Requests to the Apache HTTP Server will be redirected to the Tomcat Server (protocol: ajp). The website has more than 10'000 jsp files. We have also a google search appliance and it crawle the website every night. While it crawling, the jvm heap space rise to the max limit of 8 GB. With javamelody, I can see that the heap space increase analogue to the loaded

HashMap,HashTable,ConcurrentHashMap

限于喜欢 提交于 2019-12-22 19:03:02
HashMap,HashTable,ConcurrentHashMap 一.HashTable 底层数组+链表实现,无论key还是value都不能为null,线程安全,实现线程安全的方式是在修改数据时锁住整个HashTable,效率低,ConcurrentHashMap做了相关优化 初始size为11,扩容:newsize = olesize*2+1 计算index的方法:index = (hash & 0x7FFFFFFF) % tab.length 二.HashMap 1.7版本以前底层数组+链表实现,1.8版本后为数组+链表/红黑树,可以存储null键和null值,线程不安全 初始size为16,扩容:newsize = oldsize*2,size一定为2的n次幂 扩容针对整个Map,每次扩容时,原来数组中的元素依次重新计算存放位置,并重新插入 插入元素后才判断该不该扩容,有可能无效扩容(插入后如果扩容,如果没有再次插入,就会产生无效扩容) 当Map中元素总数超过Entry数组的75%,触发扩容操作,为了减少链表长度,元素分配更均匀 计算index方法:index = hash & (tab.length – 1) HashMap的初始值还要考虑加载因子: 哈希冲突:若干Key的哈希值按数组大小取模后,如果落在同一个数组下标上,将组成一条Entry链

Java Remove Specific Item From ConcurrentHashMap

馋奶兔 提交于 2019-12-22 04:04:30
问题 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>

Calculating average and percentiles from a histogram map?

依然范特西╮ 提交于 2019-12-22 03:20:11
问题 I have written a timer which will measure the performance of a particular code in any multithreaded application. In the below timer, it will also populate the map with how many calls took x milliseconds. I will use this map as part of my histogram to do further analysis, like what percentage of calls took this much milliseconds and etc. public static class StopWatch { public static ConcurrentHashMap<Long, Long> histogram = new ConcurrentHashMap<Long, Long>(); /** * Creates an instance of the

Java Concurrency : Volatile vs final in “cascaded” variables?

牧云@^-^@ 提交于 2019-12-21 13:09:24
问题 is final Map<Integer,Map<String,Integer>> status = new ConcurrentHashMap<Integer, Map<String,Integer>>(); Map<Integer,Map<String,Integer>> statusInner = new ConcurrentHashMap<Integer, Map<String,Integer>>(); status.put(key,statusInner); the same as volatile Map<Integer,Map<String,Integer>> status = new ConcurrentHashMap<Integer, Map<String,Integer>>(); Map<Integer,Map<String,Integer>> statusInner = new ConcurrentHashMap<Integer, Map<String,Integer>>(); status.put(key,statusInner); in case the