Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

6
  • 2
    @Viktor - If you have the (key,value) pair, map.get(key).remove(value) should do the trick as long as you leave the empty queue there as a placeholder when you remove a key and catch the exception if it's not there (including the NPE off of get). If you can't leave the queue as a placeholder, then it's difficult to ensure safety unless you do lock the entire map while you clean out the cruft.
    – Rex Kerr
    Commented Sep 3, 2010 at 13:42
  • Can't leave the queue since it will leak memory. I'm actually considering using the ConcurrentHashMap source and bend it to my will, but it seems like a very crude approach. Commented Sep 3, 2010 at 13:45
  • 1
    This may be the best solution, you can constrain locking the entire collection to only occur when you find a Queue which is empty, I'm not sure you can get away from either writing your own implementation, or changing the way you want to use this to handle this nested structure rather than a Multimap. Commented Sep 3, 2010 at 13:49
  • 3
    Can you tolerate locking the queue when you get it, and then holding the queue while you send an update to the map to empty that (k,v) pair? That is, use the lock on the queue rather than on the whole map to avoid collisions? (And add logic to handle the case where you get a queue but then, before you can lock it, it's emptied out and removed from the map--all you have to do is check when you get the lock that it is non-empty. If it is empty, assume it's been removed.)
    – Rex Kerr
    Commented Sep 3, 2010 at 13:50
  • 2
    For those looking for Viktor's implementation, you can find it as akka.util.ConcurrentMultiMap. Thanks a lot, Viktor! Commented Aug 9, 2016 at 12:10