concurrenthashmap

java面试必知必会

╄→гoц情女王★ 提交于 2019-11-28 05:42:45
java面试必知必会 对比 Vector、ArrayList、LinkedList 有何区别 Hashtable、HashMap、TreeMap 有什么不同 Hashtable、HashMap、TreeMap心得 如何保证容器是线程安全的?ConcurrentHashMap 如何实现高效地线程安全? Java 提供了哪些 IO 方式? NIO 如何实现多路复用? Java 有几种文件拷贝方式?哪一种最高效? 谈谈接口和抽象类有什么区别? 面向对象设计 面向对象编程,掌握基本的设计原则S.O.L.I.D 原则 谈谈你知道的设计模式?请手动实现单例模式,Spring 等框架中使用了哪些模式? synchronized 底层如何实现?什么是锁的升级、降级? 免责声明 对比 Vector、ArrayList、LinkedList 有何区别 这三者都是实现集合框架中的 List,也就是所谓的有序集合,因此具体功能也比较近似,比如都提供按照位置进行定位、添加或者删除的操作,都提供迭代器以遍历其内容等。但因为具体的设计区别,在行为、性能、线程安全等方面,表现又有很大不同 Vector 是 Java 早期提供的线程安全的动态数组,如果不需要线程安全,并不建议选择,毕竟同步是有额外开销的。Vector 内部是使用对象数组来保存数据,可以根据需要自动的增加容量,当数组已满时,会创建新的数组

Should you check if the map containsKey before using ConcurrentMap's putIfAbsent

青春壹個敷衍的年華 提交于 2019-11-28 02:52:32
I have been using Java's ConcurrentMap for a map that can be used from multiple threads. The putIfAbsent is a great method and is much easier to read/write than using standard map operations. I have some code that looks like this: ConcurrentMap<String, Set<X>> map = new ConcurrentHashMap<String, Set<X>>(); // ... map.putIfAbsent(name, new HashSet<X>()); map.get(name).add(Y); Readability wise this is great but it does require creating a new HashSet every time even if it is already in the map. I could write this: if (!map.containsKey(name)) { map.putIfAbsent(name, new HashSet<X>()); } map.get

ConcurrentHashMap with weak keys and identity hash?

江枫思渺然 提交于 2019-11-28 02:36:53
问题 How do I get a ConcurrentHashMap with weak keys and identity hashes in Java? I think Google Guava Collections can give such a thing, but can I get it from the standard library? What other options do I have? 回答1: I think Google Guava Collections can give such a thing, but can I get it from the standard library? The short answer to that is No. Java SE does not implement this particular combination. You could instantiate a java.util.concurrent.ConcurrentHashMap with WeakReference keys, and do

What is the difference between Collectors.toConcurrentMap and converting a Map to ConcurrentHashMap via Collectors.toMap supplier option?

混江龙づ霸主 提交于 2019-11-28 00:14:40
I want to convert a Map into a ConcurrentHashMap via Java 8 Stream and Collector interface, and there are two options I can use. The first: Map<Integer, String> mb = persons.stream() .collect(Collectors.toMap( p -> p.age, p -> p.name, (name1, name2) -> name1+";"+name2, ConcurrentHashMap::new)); And the second: Map<Integer, String> mb1 = persons.stream() .collect(Collectors.toConcurrentMap( p -> p.age, p -> p.name)); Which one is the better option? When should I use each option? There is a difference between them when dealing with parallel streams. toMap -> is a non-concurrent collector

Java容器(三)

邮差的信 提交于 2019-11-27 19:03:52
一,在Java中有普通集合、同步(线程安全)集合、并发集合。 普通集合通用性能最高,但是不保证多线程的安全性和并发的可靠性。 线程安全集合仅仅是给集合加了synchronized同步锁,严重牺牲了性能,而且对并发的效率就更低了。 并发集合则通过复杂的策略不仅保证了多线程的安全由提高了并发的效率 并发集合常见有ConcurrentHashMap、ConcurrentLinkedQueue、ConcurrentLinkedDeque,并发集合位于java.util.concurrent包下,是jdk1.5之后才有的 二,ConcurrentHashMap 是线程安全的 HashMap 的实现,默认构造同样有 initialCapacity 和 loadFactor 属性, 不过还多了一个 concurrencyLevel 属性,三属性默认值分别为 16、0.75 及 16。其内部使用锁分段技术,维持这锁 Segment 的数组,在 Segment 数组中又存放着 Entity[]数组,内部 hash 算法将数据较均匀分布在不同锁中。 put 操作: 并没有在此方法上加上 synchronized,首先对 key.hashcode 进行 hash 操作,得到 key 的 hash 值。 hash操作的算法和map也不同,根据此hash值计算并获取其对应的数组中的Segment对象

并发容器之ConcurrentHashMap

[亡魂溺海] 提交于 2019-11-27 19:02:01
  JDK5中添加了新的concurrent包,相对同步容器而言,并发容器通过一些机制改进了并发性能。因为同步容器将所有对容器状态的访问都 串行化了,这样保证了线程的安全性,所以这种方法的代价就是严重降低了并发性,当多个线程竞争容器时,吞吐量严重降低。因此Java5.0开 始针对多线程并发访问设计,提供了并发性能较好的并发容器,引入了java.util.concurrent包。与Vector和Hashtable、 Collections.synchronizedXxx()同步容器等相比,util.concurrent中引入的并发容器主要解决了两个问题: 1)根据具体场景进行设计,尽量避免synchronized,提供并发性。 2)定义了一些并发安全的复合操作,并且保证并发环境下的迭代操作不会出错。   util.concurrent中容器在迭代时,可以不封装在synchronized中,可以保证不抛异常,但是未必每次看到的都是"最新的、当前的"数据。   下面是对并发容器的简单介绍:   ConcurrentHashMap代替同步的Map(Collections.synchronized(new HashMap())),众所周知,HashMap是根据散列值分段存储的,同步Map在同步的时候锁住了所有的段

并发集合类 ConcurrentHashMap 和 CopyOnWriteArrayList

孤街浪徒 提交于 2019-11-27 18:41:22
在Java类库中出现的第一个关联的集合类是Hashtable,它是JDK 1.0的一部分。Hashtable提供了一种易于使用的、线程安全的、关联的map功能,这当然也是方便的。然而,线程安全性是凭代价换来的――Hashtable的所有方法都是同步的。此时,无竞争的同步会导致可观的性能代价。Hashtable的后继者HashMap是作为JDK1.2中的集合框架的一部分出现的,它通过提供一个不同步的基类和一个同步的包装器Collections.synchronizedMap,解决了线程安全性问题。通过将基本的功能从线程安全性中分离开来,Collections.synchronizedMap允许需要同步的用户可以拥有同步,而不需要同步的用户则不必为同步付出代价。 Hashtable和synchronizedMap所采取的获得同步的简单方法(同步Hashtable中或者同步的Map包装器对象中的每个方法)有两个主要的不足。首先,这种方法对于可伸缩性是一种障碍,因为一次只能有一个线程可以访问hash表。同时,这样仍不足以提供真正的线程安全性,许多公用的混合操作仍然需要额外的同步。虽然诸如get()和put()之类的简单操作可以在不需要额外同步的情况下安全地完成,但还是有一些公用的操作序列,例如迭代或者put-if-absent(空则放入),需要外部的同步,以避免数据争用。

Is gcc std::unordered_map implementation slow? If so - why?

风格不统一 提交于 2019-11-27 17:25:25
We are developing a highly performance critical software in C++. There we need a concurrent hash map and implemented one. So we wrote a benchmark to figure out, how much slower our concurrent hash map is compared with std::unordered_map . But, std::unordered_map seems to be incredibly slow... So this is our micro-benchmark (for the concurrent map we spawned a new thread to make sure that locking does not get optimized away and note that I never inser 0 because I also benchmark with google::dense_hash_map , which needs a null value): boost::random::mt19937 rng; boost::random::uniform_int

ConcurrentHashMap locking

爱⌒轻易说出口 提交于 2019-11-27 16:09:36
问题 I have read somewhere that in ConcurrentHashMap , the whole map object is not locked and instead a lock is made on a portion of the Map. Can somebody elaborate when does locking come into the picture? Is it right that while reading the Map there is no locking involved in it but while updating it only locking is used? 回答1: Yes, ConcurrentHashMap uses a multitude of locks (by default, 16 of them), each lock controls one segment of the hash. When setting data in a particular segment, the lock

ConcurrentHashMap原理

这一生的挚爱 提交于 2019-11-27 13:00:32
ConcurrentHashMap在JDK7和JDK8中的不同实现原理 https://blog.csdn.net/woaiwym/article/details/80675789 Java8之ConcurrentHashMap实现原理 https://blog.csdn.net/ddxd0406/article/details/81389583 ConcurrentHashMap(JDK1.8)为什么要放弃Segment https://blog.csdn.net/mian_CSDN/article/details/70185104 来源: https://www.cnblogs.com/tonggc1668/p/11364179.html