I have a class for loading from persistence items with the following method at PersistenceController
class:
Item loadItem(int id);
So now I want to implement the following method in other controller:
synchronized Item getItem(int id){
Item result= getItemFromMemory(id);
if (result==null){
result=PersistenceController.loadItem(id);
}
return result;
}
The goal is to ensure only one (or zero) instances of a item with the same id in memory at the same time. This method works but have one problem: Every load Item must wait for each other. I'd like to change the synchronization scheme in order to a call wait for other only if both have the same id.
Whats the best way to do this?