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
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
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
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-