The java meomry model mandates that synchronize
blocks that synchronize on the same monitor enforce a before-after-realtion on the variables modified within those b
Beyond the question of what the semantics of the memory model guarantees, I think there are a few problems with the code you are posting.
Lock
implementation, you don't have need to use the synchronized
block.Lock
is to do so in a try-finally block to prevent accidental unlocking of the lock (since the lock is not automatically released when entering whatever block you are in, as with the synchronized
block).You should be using a Lock
with something resembling:
lock.lock();
try {
//do stuff
}
finally {
lock.unlock();
}