Java ConcurrentHashMap学习 (一)—— HashMap Vs. ConcurrentHashMap Vs. SynchronizedMap – How a HashMap ca...

HashMap is a very powerful data structure in Java. We use it everyday and almost in all applications. There are quite a few examples which I have written before on How to Implement Threadsafe cache, How to convert Hashmap to Arraylist?

We used Hashmap in both above examples but those are pretty simple use cases of Hashmap.HashMap is a non-synchronized collection class.

Do you have any of below questions?
  • What’s the difference between ConcurrentHashMap and Collections.synchronizedMap(Map)?
  • What’s the difference between ConcurrentHashMap and Collections.synchronizedMap(Map) in term of performance?
  • ConcurrentHashMap vs Collections.synchronizedMap()
  • Popular HashMap and ConcurrentHashMap interview questions

In this tutorial we will go over all above queries and reason why and how we could Synchronize Hashmap?

Why?

The Map object is an associative containers that store elements, formed by a combination of a uniquely identify key and a mapped value. If you have very highly concurrent application in which you may want to modify or read key value in different threads then it’s ideal to use Concurrent Hashmap. Best example is Producer Consumer which handles concurrent read/write.

So what does the thread-safe Map means? If multiple threads access a hash map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externallyto avoid an inconsistent view of the contents.

How?

There are two ways we could synchronized HashMap

  1. Java Collections synchronizedMap() method
  2. Use ConcurrentHashMap

 

ConcurrentHashMap

  • You should use ConcurrentHashMap when you need very high concurrency in your project.
  • It is thread safe without synchronizing the whole map.
  • Reads can happen very fast while write is done with a lock.
  • There is no locking at the object level.
  • The locking is at a much finer granularity at a hashmap bucket level.
  • ConcurrentHashMap doesn’t throw a ConcurrentModificationException if one thread tries to modify it while another is iterating over it.
  • ConcurrentHashMap uses multitude of locks.

SynchronizedHashMap

  • Synchronization at Object level.
  • Every read/write operation needs to acquire lock.
  • Locking the entire collection is a performance overhead.
  • This essentially gives access to only one thread to the entire map & blocks all the other threads.
  • It may cause contention.
  • SynchronizedHashMap returns Iterator, which fails-fast on concurrent modification.
Now let’s take a look at code
  1. Create class CrunchifyConcurrentHashMapVsSynchronizedHashMap.java
  2. Create object for each HashTable, SynchronizedMap and CrunchifyConcurrentHashMap
  3. Add and retrieve 500k entries from Map
  4. Measure start and end time and display time in milliseconds
  5. We will use ExecutorService to run 5 threads in parallel

 

 

转载于:https://www.cnblogs.com/Guoyutian/p/5108713.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值