Java Reference
In-Depth Information
memo.add(value);
}
}
}
The
@GuardedBy
annotations on the
xPos
and
yPos
fields indicate that access to these
fields is protected by holding a lock on
this
. The
move()
method also synchronizes on
this
, which modifies these fields. The
@GuardedBy
annotation on the
memo
list indicates
that a lock on the
ArrayList
object protects its contents. The
rememberPoint()
method
also synchronizes on the memo list.
One issue with the
@GuardedBy
annotation is that it fails to indicate when there is a
relationship between the fields of a class. This limitation can be overcome by using the
SureLogic
@RegionLock
annotation, which declares a new region lock for the class to
whichthisannotationisapplied.Thisdeclarationcreatesanewnamedlockthatassociates
a particular lock object with a region of the class. The region may be accessed only when
thelockisheld.Forexample,the
SimpleLock
lockingpolicyindicatesthatsynchronizing
on the instance protects all of its state:
@RegionLock("SimpleLock is this protects Instance")
class Simple { ... }
Unlike
@GuardedBy
, the
@RegionLock
annotation allows the programmer to give an
explicit, and hopefully meaningful, name to the locking policy.
In addition to naming the locking policy, the
@Region
annotation allows a name to be
given to the region of the state that is being protected. That name makes it clear that the
state and locking policy belong together, as demonstrated in the following example:
@Region("private AircraftPosition")
@RegionLock("StateLock is stateLock protects AircraftPosition")
public final class Aircraft {
private final Lock stateLock = new ReentrantLock();
@InRegion("AircraftPosition")
private long x, y;
@InRegion("AircraftPosition")