Re: sync on local variable
On 3/25/2010 2:27 PM, Roedy Green wrote:
On Wed, 24 Mar 2010 15:11:15 -0700, Roedy Green
<see_website@mindprod.com.invalid> wrote, quoted or indirectly quoted
someone who said :
The IntelliJ code Inspector (lint) slapped my wrist for synchronising
on a local variable. What's the problem with that? The sync is on
the object, not the reference, right?
I got this response from an IntelliJ employee (probably a Russian with
ESL).
The point was synchronization only makes sense if two different
threads syncing on the same instance and question arises how safe was
an exchange, that two threads have same reference on their stacks.
Most obviously "clean" subject to synchronize on is a final field.
Synchronizing on a local reference might well be not a problem but
generally not that easy to verify for correctness and easy thing to
broke later.
One thing you can do is more the synchronization into the "Row" class,
and turn your operations into atomic operations, rather than have
external clients need to concern themselves with synchronization.
That way, the Row class can always sync on the same object (probably
"this", but not necessarily), and the client needn't care about it.
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>