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.
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?
- 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?
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.
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.
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.
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.