File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

synchronization suggestion needed

 
Ricky Murphy
Ranch Hand
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • 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;
}
}
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Tim LeMaster
Ranch Hand
Posts: 226
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic