[concurrency-interest] Re: synchronized vs ReentrantLock semantic

Doug Lea dl at cs.oswego.edu
Mon Jun 13 19:08:48 EDT 2005

> We all know that the AbstractQueuedSynchronizer provides atomicity.
> But does it also ensure visibility?
> In particular, is it safe to replace:
> synchronized (x) { ... }
> by:
> lock.lock(); try { ... } finally { lock.unlock(); }

Yes! That's what the Lock spec says. Rely on it.

> (The contract of monitorExit ensures visibility, so I would expect
> the answer to be yes based on the Lock javadoc, but it does not
> follow from the AbstractQueuedSynchronizer javadoc, or the source
> code, as far as I can see).

It does follow from the javadoc spec. See the Java
memory model (in the 3rd edition of JLS
to help decode it and to see how getState/setState/compareAndSetState
suffice to obtain lock memory semantics.


More information about the Concurrency-interest mailing list