• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

synchronization suggestion needed

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to implement some cache in my application. The situation is like this, objects can be added to a cache, the cache can be invalidated on some conditions, and some user may retrieve some object from the cache. I don't want the process to break with all these activities may happen all at the same time. I therefore thought about synchronization. However, unable to find a better way to apply synchronization to it and keep a best performance possible. Can someone please help what should I do to apply synchronization in this case. I put some pseudo-code below for my CacheUtility class:

public class CacheUtililty()
{
public void addCache(Cache myCache, Object obj)
{

//add obj to myCache;
}

public void invalidCache(Cache myCache)
{
// invalidate myCache;
}

public Object getCache(Cache myCache, String key)
{
//Object obj=myCache.getCache(key);
//return obj;
}
}
 
Ranch Hand
Posts: 443
3
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just synchronize your methods and move to a 1.6 JVM the work the JVM does to keep synchronization cost down is amazing.

If you still think you have a preformance issue , profile it and if you think its the locks we can then play with read and write locks (java 1.5) etc I suspect you won't require this. A lot of the further optimisations depend on how many reads to writes you do or make the code really hard to understand for little preformance benefit.

Also with caches have a look at SoftReferences I used them recently with a cache and they can make your cache very flexible.
 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks like to me these methods should be part of the Cache class. That instead of



You would have



If you put the code into the CacheUtility class, you deal with the synchronizing there, however what about code that doesn't use CacheUtility and just goes after the Cache object itself?
 
Ricky Murphy
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your replies. I looked it up and thought the ReentrantReadWriteLock seems to be suitable for my case. I potentially have many concurrent reads and very few concurrent writes. What I don't want to see is that the read get null while cache is being invalidated. the mutual exclusive lock (apply "synchronize" to all three methods) seems cause a performance issue in my case. Does that make sense? comments?

Also, yes, those methods are supposed to be part of the Cache framework. we just want to have more control of what the Cache framework provides us so we put a wrapper around that to make it thread safe. By the way, we are using ehcache.

Thank you

-Rick
 
And then we all jump out and yell "surprise! we got you this tiny ad!"
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic