I was checking some old code of ours. It is multithreaded, but the author commited some beginner mistakes, for example not synchronizing booleans and so. But this piece of code might even work, but it seems bizarre to me:
Each of these methods locks object and in the synch block it assigns new object to the reference. Both integers are carrying two roles now: semantics and synchronization.
However, after some head scratching I figured that it should work. I wrote test and it passes. I'm still inclined to change the code since it looks more like a contrived example for programming exam as it just makes you wonder. This can't be an example of idiomatic MT programming, right? If not, I will just use AtomicInteger, probably.