I have a data warehouse and I want to synchronize the changes associated with one specific object at a time.
class DataStore { Map<ID, DataObject> objects =
That is, I do not want to have one lock in my data warehouse, since changes in different data objects are completely orthogonal. Instead, I want to be able to block, which applies to only one data object.
Each data object has a unique identifier. One way is to create an ID => Lock card and synchronize with one lock object associated with the identifier. Another way is to do something like:
synchronize(dataObject.getId().toString().intern()) {
However, this is similar to a memory leak - inline strings can never be collected.
Another idea is to synchronize with the data object itself; however, what if you have an operation when the data object does not yet exist? For example, how will a method such as addDataObject(DataObject) be synchronized?
So, how can I write a function f(s) , where s is a String , so that f(s)==f(t) if s.equals(t) memory-safe way?
source share