How does DelegatingVehicleTracker (p. 65 Goetz) return a “live” view?

前端 未结 3 1266
臣服心动
臣服心动 2021-01-12 22:32

On page 65 and 66 of Java Concurrency in Practice Brian Goetz lists the following code:

@ThreadSafe
public class DelegatingVehicleTracker {
private final Con         


        
3条回答
  •  独厮守ぢ
    2021-01-12 22:59

    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)
    

提交回复
热议问题