wood burning stoves*
The moose likes Threads and Synchronization and the fly likes synchronization suggestion needed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "synchronization suggestion needed" Watch "synchronization suggestion needed" New topic

synchronization suggestion needed

Ricky Murphy
Ranch Hand

Joined: Oct 16, 2007
Posts: 31
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

Joined: Oct 26, 2003
Posts: 407

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.

"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
Tim LeMaster
Ranch Hand

Joined: Aug 31, 2006
Posts: 226
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

Joined: Oct 16, 2007
Posts: 31
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

I agree. Here's the link: http://aspose.com/file-tools
subject: synchronization suggestion needed
Similar Threads
inner class - ClassNotFoundException
compile-time type checking
sorry... my previous mail got posted halfway...completing now
TypeFactory can it be done in java?
Object Caching?