On page 65 and 66 of Java Concurrency in Practice Brian Goetz lists the following code:
@ThreadSafe
public class DelegatingVehicleTracker {
private final Con
getLocations()
will return a read-only map which will reflect updates after getLocations()
is called.
getLocationsAsStatic()
on the other hand, will return a read-only-snapshot (aka deep copy) of the Location map at the time getLocationsAsStatic()
is called.
To illustrate:
Map locs = // a map with point A(1,1) in it
DelegatingVehicleTracker tracker = DelegatingVehicleTracker(locs);
Map snapshot = getLocationsAsStatic();
Map live = getLocations();
Point newB = // a point A(2,2)
tracker.setLocation(newB);
snapshot.get("A"); // will read A(1,1)
live.get("A"); // will read A(2,2)