Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
java_concurrency_in_practice.pdf
Скачиваний:
104
Добавлен:
02.02.2015
Размер:
6.66 Mб
Скачать

5288B5287B5286B5249B5230B5229B5200B5148B4916B4722B4721B4720B4719B4594B4593B4569B4568B4567B45

66B4565B4564B4427B4426B4410B4409B4345B4218B4217B4216B4215B3636B3635B3469B3468B3465B3455B3454

43

B3453B3449B3221B3220B3219B3214B3059B3058B3057B2535B2534B2190B2189B2188B2 16BChapter 4.

Composing Objects 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 an unmodifiable but "live" view of the vehicle locations. This means that if thread A calls getLocations and thread B later modifies the location of some of the points, those changes are reflected in the Map returned to thread A.

As we remarked earlier, this can be a benefit (more up to date data) or a liability (potentially inconsistent view of the fleet), depending on your requirements.

If an unchanging view of the fleet is required, getLocations could instead return a shallow copy of the locations map.

Since the contents of the Map are immutable, only the structure of the Map, not the contents, must be copied, as shown in Listing 4.8 (which returns a plain HashMap, since getLocations did not promise to return a thread safe Map).

Listing 4.7. Delegating Thread Safety to a ConcurrentHashMap.

@ThreadSafe

public class DelegatingVehicleTracker {

private final ConcurrentMap<String, Point> locations; private final Map<String, Point> unmodifiableMap;

public DelegatingVehicleTracker(Map<String, Point> points) { locations = new ConcurrentHashMap<String, Point>(points); unmodifiableMap = Collections.unmodifiableMap(locations);

}

public Map<String, Point> getLocations() { return unmodifiableMap;

}

public Point getLocation(String id) { return locations.get(id);

}

public void setLocation(String id, int x, int y) {

if (locations.replace(id, new Point(x, y)) == null) throw new IllegalArgumentException(

"invalid vehicle name: " + id);

}

}

Listing 4.8. Returning a Static Copy of the Location Set Instead of a "Live" One.

public Map<String, Point> getLocations() { return Collections.unmodifiableMap(

new HashMap<String, Point>(locations));

}

4.3.2. Independent State Variables

The delegation examples so far delegate to a single, thread safe state variable. We can also delegate thread safety to more than one underlying state variable as long as those underlying state variables are independent, meaning that the composite class does not impose any invariants involving the multiple state variables.

VisualComponent in Listing 4.9 is a graphical component that allows clients to register listeners for mouse and keystroke events. It maintains a list of registered listeners of each type, so that when an event occurs the appropriate listeners can be invoked. But there is no relationship between the set of mouse listeners and key listeners; the two are independent, and therefore VisualComponent can delegate its thread safety obligations to two underlying thread safe lists.

VisualComponent uses a CopyOnWriteArrayList to store each listener list; this is a thread safe List implementation particularly suited for managing listener lists (see Section 5.2.3). Each List is thread safe, and because there are no constraints coupling the state of one to the state of the other, VisualComponent can delegate its thread safety responsibilities to the underlying mouseListeners and keyListeners objects.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]