Java Reference
In-Depth Information
DelegatingVehicleTracker
in
Listing 4.7
does not use any explicit synchronization;
all access to state is managed by
ConcurrentHashMap
, and all the keys and values of the
Map
are immutable.
Listing 4.7. Delegating Thread Safety to a
ConcurrentHashMap
.
If we had used the original
MutablePoint
class instead of
Point
, we would be breaking
encapsulation by letting
getLocations
publish a reference to mutable state that is not
thread-safe. Notice that we've changed the behavior of the vehicle tracker class slightly;
while the monitor version returned a snapshot of the locations, the delegating version returns