I have created a Cache
object which stores a String
as the key and a serialized object as the value.
Cache(String--->Object)
I am trying to run three Akka threads which retrieve and write into the same Ehcache object in a synchronized way.
Thread 1- synchronized (LockForEhcache){
serializedObj = cachename.get("key"); //--- this returns an Object
}
//modify the serializedObj here....
//Again store the modify Object in the Cache
synchronized (LockForEhcache){
cachename.clear();
cachename.put("key",serializedObj);
Thread 2- synchronized (LockForEhcache){
serializedObj = cachename.get("key"); //--- this returns null
}
Thread 3- synchronized (LockForEhcache){
serializedObj = cachename.get("key"); //--- this returns null
}
But only one thread gets the value stored in the Cache
. For the rest of the threads, it throws a NullPointerException
. I can't figure out why.