Why would I place a synchronized block within a single-threaded method? Why would I place a synchronized block within a single-threaded method? multithreading multithreading

Why would I place a synchronized block within a single-threaded method?


According to the Java memory model there is a happens-before relationship between a volatile write and a subsequent volatile read, which means that m = currentMap; is guaranteed to see everything that happened before currentMap = newMap;, the synchronized block is not needed.

Not only that, but it does absolutely nothing, the:

this must be synchronized because of the Java memory model

and

After the above synchronization block, everything that is in the HashMap is visible outside this thread

comments are wrong. According to the Java memory model there is a happens-before relationship only when both threads are synchronized; using it as in the article does absolutely nothing according to the JMM (some JVM implementation can do something and maybe some IBM JVM implementation in 2007 performed some kind of synchronization in this case, but JMM doesn't require it).

In conclusion, the IBM article is simply wrong.