Java Reference
In-Depth Information
Similarly, the updater threads would modify vehicle locations with data received from GPS
devices or entered manually by a dispatcher through a GUI interface:
Since the view thread and the updater threads will access the data model concurrently, it must
be thread-safe. Listing 4.4 shows an implementation of the vehicle tracker using the Java
monitor pattern that uses MutablePoint in Listing 4.5 for representing the vehicle loca-
tions.
Even though MutablePoint is not thread-safe, the tracker class is. Neither the map nor
any of the mutable points it contains is ever published. When we need to a return vehicle
locations to callers, the appropriate values are copied using either the MutablePoint copy
constructor or deepCopy , which creates a new Map whose values are copies of the keys and
values from the old Map . [3]
This implementation maintains thread safety in part by copying mutable data before returning
it to the client. This is usually not a performance issue, but could become one if the set of
vehicles is very large. [4] Another consequence of copying the data on each call to getLoca-
tion is that the contents of the returned collection do not change even if the underlying loc-
ations change. Whether this is good or bad depends on your requirements. It could be a bene-
Search WWH ::




Custom Search