• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ConcurrentHashMap or LinkedHashMap

 
rohit chavan
Ranch Hand
Posts: 132
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am trying to implement a simple cache mechanism using a HashMap implementation.
I would like to know the difference between ConcurrentHashMap and LinkedHashMap, to understand which one is better for caching in a multithreaded env.
I have already gone through some javadocs available, and it has added to my curiosity to know, that which implementation to chose for a synchronized read and write (get and put).
Appreciate your views on the same.

Thanks,
Rohit Chavan
 
Stephan van Hulst
Bartender
Pie
Posts: 5349
50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What makes you consider these two maps? After you've picked on or the other, how are you intending to use them in your caching mechanism? What differences have you figured out so far?
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will have to come up with the requirements for your storage structure to figure out which one is best. There are two big differences:

1) The LinkedHashMap is ordered but not thread safe

2) The ConcurrentHashMap is thread safe but not ordered

If you need an ordered thread safe map, then maybe ConcurrentSkipListMap might be a better choice (but maybe not...).

If you wanted the ordering of LinkedHashMap in a thread safe structure, your concerns should be:
- How much work would it take to make LinkedHashMap thread safe?
- Do you trust yourself to be able to make it thread safe?
- Can you make it thread safe and still efficient?

versus
- How much work would it take to make ConcurrentSkipListMap (or ConcurrentHashMap) sort like the LinkedHashMap? At first blush, this might seem easy (CSKLM uses a comparator, so just make a comparator for access time) but it won't be (you would be sorting on something other than the Key (insertion/access order), your structure would have to change with access, not just insertion, iteration would be affected...).
- Is the Map you come up with efficient enough to use?
 
rohit chavan
Ranch Hand
Posts: 132
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:What makes you consider these two maps? After you've picked on or the other, how are you intending to use them in your caching mechanism? What differences have you figured out so far?


I am considering these two maps as I was looking for an LRU implementation (which made me consider LinkedHashmap) , I have serveral types of key value pairs that I am looking ahead to cache, in order to avoid DB hits. I am planning to create a map for each category (around 4-5 maps), for an application which has multiple hits (in some thousands per day).

I was unable to decide on one because if I use LHM I need to synchronize the access to it, if I use CHM I get concurrencyLevel which helps me in deciding the estimated number of concurrently updating threads.

 
rohit chavan
Ranch Hand
Posts: 132
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve,
I am not sure if the concurrency facilities provided would be useful in the scenatio.
Faster in-order traversal at extra cost for insertion is how I can see this Class.
Please put some more light on this if possible.

Thanks,
Rohit Chavan
 
Paul Clapham
Sheriff
Pie
Posts: 20733
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rohit chavan wrote:I am not sure if the concurrency facilities provided would be useful in the scenatio.


Does this mean that you don't know whether the design includes accessing that map concurrently from more than one thread? Then perhaps the design needs to be clarified, so that it's possible to answer that question. At any rate you can't decide what type of map to use without knowing the conditions under which it will be used -- and that means understanding the design of the application.
 
Avi Stramer
Greenhorn
Posts: 6
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Option 1: Don't reinvent the wheel. It's very tricky to make a threadsafe LRU cache implementation. Why not just use something like ehcache? Pretty easy to configure it to function as an LRU cache (http://ehcache.org/documentation/apis/cache-eviction-algorithms).

Backup Option: If you don't want to follow option 1, sounds like your app has very low volume of hits (few thousand a day is tiny). Just wrap all access to the LinkedHashMap with a ReadWriteLock and ensure all gets/sets acquire the read/write lock respectively. The overhead of a readwritelock is quite small for an app like you are describing.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic