• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Grappling with ConcurrentHashMap

 
Steven Rodeo
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


HI folks,

Apologies, if this is trivial.

I am implementing a cache.

The KEY being a "String" and the VALUE being an List. I wrote this code to this effect.

Is this function thread safe ?. For instance what happens if another thread comes by and deletes a given mapping for a key.



Thanks Much
-SM
 
Winston Gutkowski
Bartender
Pie
Posts: 10277
60
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steven Rodeo wrote:I am implementing a cache.
The KEY being a "String" and the VALUE being an List.

I guess my first question would be: why a List?
Since Lists allow duplicates, value could be added many times for the same key. Is that what you want?

I wrote this code to this effect.
Is this function thread safe ?.

Looks it; however, it seems a bit tortuous. How about just:
Winston
 
Winston Gutkowski
Bartender
Pie
Posts: 10277
60
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steven Rodeo wrote:Is this function thread safe?

It also occurred to me that if each entry in your cache was also a Concurrent collection, you could do the whole thing in one line, viz:

Winston
 
Mike Simmons
Ranch Hand
Posts: 3041
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:

But putIfAbsent() returns null if the key was not previously mapped.
 
Steven Rodeo
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is going to throw a NullPointerException ?. I see this from the javadocs



  • Except that the action is performed atomically.

    Specified by: putIfAbsent in interface ConcurrentMap<K,V>

    Parameters:
    key - key with which the specified value is to be associated.
    value - value to be associated with the specified key.

    Returns:
    previous value associated with specified key, or null if there was no mapping for key.

    Throws:
    NullPointerException - if the specified key or value is null.
  •  
    Winston Gutkowski
    Bartender
    Pie
    Posts: 10277
    60
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Mike Simmons wrote:
    Winston Gutkowski wrote:

    But putIfAbsent() returns null if the key was not previously mapped.

    Oh yeah. Duh. Seems to me they might have missed a beat there - I guess it's to keep it consistent with HashMap, but why return null when you don't have to?

    @Steven: My apologies. I guess it has to be:but I am right that if you use a Concurrent dataset you don't need the synchronized block.

    Winston
     
    Jeff Verdegan
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I usually do it as:



    The tradeoff between an unneeded lookup and an unneeded object creation may be a bit of premature optimization in either direction, but I'll usually go with the first case (extra lookup).
     
    Winston Gutkowski
    Bartender
    Pie
    Posts: 10277
    60
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jeff Verdegan wrote:The tradeoff between an unneeded lookup and an unneeded object creation may be a bit of premature optimization in either direction, but I'll usually go with the first case (extra lookup).

    Yeah, I guess if it's a cache, the first style might save you creating a lot of empty Lists (or V's) unnecessarily.

    Winston
     
    Pat Farrell
    Rancher
    Posts: 4678
    7
    Linux Mac OS X VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    First thing I'd ask is why are you implementing a cache? There are lots of existing implementations that are free, open source, debugged, documented, etc.

    So if you want to learn, lets keep going. But if you just want to use one, I'd start with the Google Guava com.google.common.cache.* classes.

    Once you get the fundamentals working, you really need to thing long and hard about what strategies you want to use, when do you kick entries out of the cache, how often do you kick one or more out, what is the loading strategy, etc.

    The initial discussion of them usually start with a last used goes out first strategy, but that is not optimal in many real world usages.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic