This content has been marked as final. Show 3 replies
Seems perfectly reasonable to me.
First of all you don't synchronize on variables, you synchronize on objects. And since "final" refers to the variable, that means that "final" doesn't apply to your question.
And second, it doesn't matter whether the object you choose for your synchronization monitor is mutable or not. All that matters is that all code in the critical sections (the ones which should only be run by one thread at a time) are protected by the same monitor.
And in this case, since the critical sections are all for the purpose of modifying the map, it makes perfect sense to use the map itself as the monitor. You could certainly use some other object as the monitor, i.e. the "dummy Object" you referred to, but then the coder has to remember to use the same dummy object to protect every critical section. That's an extra thing to keep track of and potentially get wrong.