Re: sync on local variable
On 3/26/2010 3:19 PM, Eric Sosman wrote:
On 3/26/2010 4:18 PM, Daniel Pitts wrote:
[...]
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.
Are you sure he can do that? He calls three methods on
the row instance, in two synchronized blocks. Split them into
three blocks, and the pair that were together may no longer see
their row in the same state, because something may happen after
one method releases the lock and before the next acquires it.
The first call is synchronized separately. The second and third call
are in one synchronized block. The "atomicity" of those two calls can
be handled in one method:
class MarkedUrl {
private final VersionURL url;
private final Marker marker;
// appropriate contstructor and getters.
}
public class AppToWatch {
public syncronized MarkedUrl getMarkedUrl() {
return new MarkedUrl(versionUrl, marker);
}
}
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>