concurrenthashmap

Does a ConcurrentHashMap need to be wrapped in a synchronized block?

折月煮酒 提交于 2019-12-08 22:48:13
问题 Do all non-retreival operations on a ConcurrentHashMap ( put() , remove() etc.) need to be wrapped in a synchronized(this) block? I understand that all of these operations are thread-safe, so is there any real benefit/need in doing so? The only operations used are put() and remove() . protected final Map<String, String> mapDataStore = new ConcurrentHashMap<String, String>(); public void updateDataStore(final String key, final String value) { ... synchronized (this) { mapDataStore.put(key,

Weakly consistent iterator by ConcurrentHashMap

孤人 提交于 2019-12-08 08:15:33
问题 The Java Concurrency in Practice mentions that: The iterator returned by the ConcurrentHashMap are weakly consistent than fail-fast. A weakly consistent iterator can tolerate the concurrent modifications, traverses elements as they existed when the iterator was constructed, and may (but is not guaranteed to) reflect modifications to the collection after the construction of the iterator. How making the iterator weakly consistent or fail-safe helps in the concurrent environment because still

Thread-safe map with null-key capability

时间秒杀一切 提交于 2019-12-07 04:37:48
问题 I need a multi-threaded Map object to use in my web server's caching, and I need to have null keys. HashMap allows me to have null keys, but ConcurrentHashMap doesn't. I tried to create a synchronized version of HashMap using Collections.synchronizedMap(new HashMap()) but it doesn't accept null keys either. Is there any alternative that I can use, without having to implement some way to wrap the null keys? 回答1: The Map returned by Collections.synchronizedMap supports all of the features of

Is this code a thread-safe one?

早过忘川 提交于 2019-12-07 03:06:29
问题 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,

ConcurrentHashMap源码跟踪记录

China☆狼群 提交于 2019-12-06 23:15:20
concurrentHashMap源码解读 主要理解几个问题1 ConcurrentHashMap如何实现分段锁2 存取数据是否读写分离 1 分段锁的实现 ①创建默认长度16的segments数组 ② put方法 1 从segments数组获取segment对象若对象为空则创建segment对象 2 将元素存入segment对象的HashEntry<K,V>table数组下标 1) 计算元素存入segment对象的HashEntry<K,V>table数组下标并判断元素是否已经存在 2)创建HashEntry对象并增加元素 ③ remove方法 remove方法和put方法类似 1计算hash值从segments取出segment 2segment加锁并计算hash值从table中取出元素,然后unsafe将根据下标设置为null即可 ④ get方法 get方法读取数据不加锁,可以多线程并发访问 总结: ConcurrentHashMap实现分段锁的原理 1 在构造方法默认初始化长度16(使用其他的构造方法可以指定长度)的segments数组 2 在put中取出单个segment(segment为空则创建segment对象),然后对segment加锁,再然后元素存入segment的table数组中

Wrong implementation of Oracle Java ConcurrentHashMap?

怎甘沉沦 提交于 2019-12-06 20:33:27
问题 I am testing ConcurrentHashMap on Oracle's Java 8 implementation: ConcurrentMap<String, String> concurrentMap = new ConcurrentHashMap<>(); String result = concurrentMap.computeIfAbsent("A", k -> "B"); System.out.println(result); // "B" result = concurrentMap.putIfAbsent("AA", "BB"); System.out.println(result); // null The Javadoc of computeIfAbsent does say that Implementation Requirements: The default implementation is equivalent to the following steps for this map, then returning the

如何设计一个本地缓存

耗尽温柔 提交于 2019-12-06 20:23:31
前言 最近在看Mybatis的源码,刚好看到缓存这一块,Mybatis提供了一级缓存和二级缓存;一级缓存相对来说比较简单,功能比较齐全的是二级缓存, 沈阳SEO 基本上满足了一个缓存该有的功能;当然如果拿来和专门的缓存框架如ehcache来对比可能稍有差距;本文我们将来整理一下实现一个本地缓存都应该需要考虑哪些东西。 考虑点 考虑点主要在数据用何种方式存储,能存储多少数据,多余的数据如何处理等几个点,下面我们来详细的介绍每个考虑点,以及该如何去实现; 1.数据结构 首要考虑的就是数据该如何存储,用什么数据结构存储,最简单的就直接用Map来存储数据;或者复杂的如redis一样提供了多种数据类型哈希,列表,集合,有序集合等,底层使用了双端链表,压缩列表,集合,跳跃表等数据结构; 2.对象上限 因为是本地缓存,内存有上限,所以一般都会指定缓存对象的数量比如1024,当达到某个上限后需要有某种策略去删除多余的数据; 3.清除策略 上面说到当达到对象上限之后需要有清除策略,常见的比如有LRU(最近最少使用)、FIFO(先进先出)、LFU(最近最不常用)、SOFT(软引用)、WEAK(弱引用)等策略; 4.过期时间 除了使用清除策略,一般本地缓存也会有一个过期时间设置,比如redis可以给每个key设置一个过期时间,这样当达到过期时间之后直接删除,采用清除策略+过期时间双重保证; 5.线程安全

“金九银十”已过,总结我的天猫、蚂蚁、头条面试经历(Java岗)

此生再无相见时 提交于 2019-12-06 12:29:18
跳槽时时刻刻都在发生,但是我建议大家跳槽之前,先想清楚为什么要跳槽。切不可跟风,看到同事一个个都走了,自己也盲目的开始面试起来(期间也没有准备充分),到底是因为技术原因(影响自己的发展,偏移自己规划的轨迹),还是钱给少了,不受重视。 准备不充分的面试,完全是浪费时间,更是对自己的不负责(如果title很高,当我没说)。 今天给大家分享我在这次“金九银十”期间跳槽面试的几个大厂经历,总结了这次跳槽中整理的Java面试合集,其中大部分都是这次面试过程中的面试题,可以对照这查漏补缺。 天猫 天猫一面(55分钟) 自我介绍 HashMap实现原理,ConcurrentHashMap实现原理 红黑树,为什么允许局部不平衡 TCP,UDP区别,为什么可靠和不可靠 一次HTTP请求的全过程,包括域名解析、定位主机等 TCP三次握手 MySQL事务是什么?四大特性,四大隔离级别 ConcurrentHashMap和Hashtable区别 spring IOC和AOP,以及各有什么优点 有哪几种常用的线程池 什么情况下使用Runnable和Thread创建线程,Runnable和Callable的区别 线程方法中的异常如何处理,副线程可以捕获到吗 synchronized和锁的区别,什么情况下使用synchronized和ReentrantLock JVM的对象分配在哪个区

Use of ConcurrentHashMap eliminates data-visibility troubles?

こ雲淡風輕ζ 提交于 2019-12-06 11:40:40
问题 I've read through Java Concurrency in Practice and am left with this question: when I use a ConcurrentHashMap, what data concurrency issues discussed in Part One of the book do I still need to worry about? Here are a couple of examples from one of my programs: 1. Current position of a trader (a shared integer where 'integer' is the mathematical term) This number represents what a trader object currently owns and defines its state. It must read its position to know what to do (look to start a

Fastest way to initialize a value for ConcurrentHashMap

允我心安 提交于 2019-12-06 10:34:35
问题 ConcurrentHashMap is often used in concurrent environments for aggregation of some events under a key - like counting hits for some string values. In case we don't know the keys in advance we need to have a good way to initialize key on need, it should to be fast and safe in terms of concurrency. What is the best pattern (in terms of efficiency) for this problem? I will use a model map with <String, AtomicInteger> declared like: ConcurrentHashMap<String, AtomicInteger> map = new