• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Grappling with ConcurrentHashMap

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
     
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
     
    Rancher
    Posts: 4803
    7
    Mac OS X VI Editor Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
    reply
      Bookmark Topic Watch Topic
    • New Topic