1

I'm reaching back to a previous question I had: Frequently Used metadata Hashmap

However, this time how would I adapt that solution to work with ConcurrentMap? LinkedHashMap isn't a synchronized collection, and due to performance reasons I don't want to wrap it in sycronizedMap. Is there an alternative [concurrent friendly] solution to solve this problem?

The original problem was that I need a hash map that gets rid of the least used items after so many entries.

Community
  • 1
  • 1
monksy
  • 14,156
  • 17
  • 75
  • 124
  • 3
    Do you actually get performance problems if you wrap it in a synchronizedMap? Or is this a case of `premature optimisation`? –  Nov 18 '11 at 07:02
  • 1
    You may want to check out: http://code.google.com/p/concurrentlinkedhashmap/wiki/Design – Deco Nov 18 '11 at 07:06
  • Why don't you use `ConcurrentHashMap` from `java.util.concurrent`? – pushy Nov 18 '11 at 07:42
  • I haven't had to test out the performance of sychMap ... theres a bench mark in Currencency in Practice by Gorltz/Bloche, et al. The design of concurrentmap is rather different. – monksy Nov 18 '11 at 07:46
  • 1
    ConcurrentHashMap is particularly useful is you have multiple threads and all they do is access one Map. If you have multiple threads which spend most of their time doing something else. i.e. a relatively low percentage of the time there is two threads trying to access the same map, then it doesn't matter which one you use. – Peter Lawrey Nov 18 '11 at 07:58
  • I'm using it for a caching mechanism in a multithreaded environment, so keeping the cache at a reasonable size is a concern, and allowing it to be [safely] multithreaded is absolutely required. Peter: had no idea that you were an stackoverflower... I'm a regular follower of your blog [since july] – monksy Nov 18 '11 at 19:48

1 Answers1

1

The Guava MapMaker and CacheBuilder classes can generate concurrent maps with an LRU eviction policy.

The CacheBuilder class is preferred for caching use-cases, as the Guava developers are in the process of stripping the cache support methods out of MapMaker.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • 2
    [CacheBuilder](http://guava-libraries.googlecode.com/svn/trunk/javadoc/com/google/common/cache/CacheBuilder.html) is our replacement for MapMaker and should be preferred. – Ben Manes Nov 18 '11 at 07:28
  • I'll accept this answer only because it leads to CacheBuilder. – monksy Nov 19 '11 at 21:24