File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes ConcurrentHashMap or LinkedHashMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "ConcurrentHashMap or LinkedHashMap" Watch "ConcurrentHashMap or LinkedHashMap" New topic
Author

ConcurrentHashMap or LinkedHashMap

rohit chavan
Ranch Hand

Joined: Oct 08, 2010
Posts: 131

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

Joined: Sep 20, 2010
Posts: 3594
    
  14

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

Joined: Jan 28, 2003
Posts: 4167
    
  21

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?


Steve
rohit chavan
Ranch Hand

Joined: Oct 08, 2010
Posts: 131

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

Joined: Oct 08, 2010
Posts: 131

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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Aug 23, 2012
Posts: 6

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ConcurrentHashMap or LinkedHashMap
 
Similar Threads
Need to store the result obtained within a while loop for further usage
Exam 7, Q19,Q30 Dan Chisholm
collections framework
HashMap and LinkedHashMap
What to lock if I want to allow multi-thread access different keys in the LinkedHashMap