I am using JCS for caching in my application.Recently some errors are occuring where concurrent access to data in the cache results in null value i.e one thread writes to the cache and one thread reads to the cache.I want to know whether JCS inherently supports thread safe implementations when writing and reading from the cache. I also want to know how to make my implementation thread safe.Because I have multiple classes writing to the cache,say PutData which implements Runnable is used for writing to the cache and GetData which also implements Runnable for reading from the cache,so making the method synchronized has no meaning and also making them atomic also is not meaningful since the data is not shared among the classes and I pass around the data object to the individual classes.BTW I am using a POJO serialiazed class. Is there anyway to overcome this,or do I have to change my implementation in such a way that it forcefully completes writing and then reading,which is foolish I think.
This is more of like a producer-consumer problem except in the fact that my consumer thread here is not consuming data,but just reading data. So synchronizing does guarantee that only one thread writes to the cache,but that does not solve my problem because the other thread accesses objects of different key.
Expecting your answers, Thanks, Madhu.
I don't know JCS but you can synchronize on objects, so you might want to synchronize on the cache object.
Someting like this:
public void putToCache(...) {
synchronized (cache) { //cache is your actual cache instance here
//put to cache here
}
}