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.
marking as final it will be an varible.
Can you explain in detail.
marking as final it will be a variable.Meaningless.
Can you explain in detail.Explain what in detail? Seems to me the answer was very detailed already.