1、前言

书中在解释Java监视器模式的时候使用了一个车辆追踪器例子,根据不同的使用场景给出了不同的实现和优化。

2、监视器模式示例

实现一个调度车辆的“车辆追踪器”,每台车使用一个String对象标识,并且拥有一个相应的位置坐标(x,y)。由于运行在多线程的场景下,对外暴露的接口需要保证线程安全。

需要提供的接口包括:

获取所有车辆标识和位置

读取某个车辆位置

更新某个车辆位置

下面给出第一种实现:

@ThreadSafepublic classMonitorVehicleTracker {
@GuardedBy("this")private final Maplocations;public MonitorVehicleTracker(Mappoints) {
locations=deepCopy(points);
}public synchronized MapgetLocations() {returndeepCopy(locations);
}public synchronizedMutablePoint getLocation(String key) {
MutablePoint point=locations.get(key);return point == null ? null : newMutablePoint(point);
}public synchronized void setLocation(String id, int x, inty) {if (id == null) {return;
}
MutablePoint point=locations.get(id);if (point == null) {throw new IllegalArgumentException("No such ID: " +id);
}
point.setPoint(x, y);
}private Map deepCopy(Mappoints) {if (points == null) {returnMaps.newHashMap();
}
Map result =Maps.newHashMapWithExpectedSize(points.size());for(String key : points.keySet()) {
result.put(key,newMutablePoint(points.get(key)));
}returnCollections.unmodifiableMap(result);
}
}
@NotThreadSafepublic classMutablePoint {private intx, y;public MutablePoint(int x, inty) {this.x =x;this.y =y;
}publicMutablePoint() {
}publicMutablePoint(MutablePoint point) {if (point == null) {throw new IllegalArgumentException("param is null");
}int[] pointArray =point.getPointArray();
x= pointArray[0];
y= pointArray[1];
}public int[] getPointArray() {int[] ret = new int[2];
ret[0] =x;
ret[1] =y;returnret;
}public void setPoint(int x, inty) {this.x =x;this.y =y;
}
}

首先需要定义一个表示位置的类MutablePoint,该类是可变的,非线程安全的。车辆追踪器的逻辑实现在类MonitorVehicleTracker,提供了所需的三种接口逻辑。MonitorVehicleTracker是一个线程安全类,通过java内置锁(synchronized)和深度拷贝实现,返回的位置信息拷贝了当前的数据,包括车辆表示和对应的位置信息。这种实现方式得到的位置信息是当前的快照,这样的数据结果是否合适取决于你的需求。

上面这个实现使用了深度拷贝的方式,这种方式在车辆数量非常大的时候存在性能问题。那么是否可以直接返回原有的数据呢,答案是不可以,如果直接返回,这样意味着直接发布了不支持线程安全的HashMap结构,该数据会在多个线程将共享。

那么我们是否有其他方式解决这个问题呢。一种方案是将线程安全的能力委托给类中内部组件,而java提供了线程安全的HashMap-concurrentHashMap(HashTable、Collections.synchronizedMap()性能不及ConcurrentHashMap)

下面给出第二种实现:

@ThreadSafepublic classMonitorVehicleTracker {private final ConcurrentHashMaplocations;private final MapunmodifiedLocations;public MonitorVehicleTracker(MappointMap) {
locations= new ConcurrentHashMap<>(pointMap);
unmodifiedLocations=Collections.unmodifiableMap(locations);
}public MapgetLocations() {returnunmodifiedLocations;
}public void setLocation(String id, int x, inty) {if(StringUtils.isBlank(id)) {return;
}if (locations.replace(id, new ImmutablePoint(x, y)) == null) {throw new IllegalArgumentException("No such ID: " +id);
}
}publicImmutablePoint getLocation(String id) {if(StringUtils.isBlank(id)) {throw new IllegalArgumentException("param is null");
}returnlocations.get(id);
}
}
@Immutable
@ThreadSafepublic classImmutablePoint {private final intx, y;public ImmutablePoint(int x, inty) {this.x =x;this.y =y;
}publicImmutablePoint(MutablePoint point) {if (point == null) {throw new IllegalArgumentException("param is null");
}int[] pointArray =point.getPointArray();
x= pointArray[0];
y= pointArray[1];
}public int[] getPointArray() {int[] ret = new int[2];
ret[0] =x;
ret[1] =y;returnret;
}
}

第二个实现中,MonitorVehicleTracker类的线程安全能力委托给内部组件。因为ConcurrentHashMap本身是一个线程安全的HashMap,所以无需进行深度拷贝,直接在线程间共享该数据结构即可。从上面的实现可以看到,位置信息使用ImmutablePoint而不是MutablePoint,这是因为位置信息也会发布出去,也可能会在线程间共享,而ConcurrentHashMap只能保证自身操作的线程安全。ConcurrentHashMap的key、value都需要是线程安全的,ImmutablePoint使用不变性提供了线程安全,String可以认为是常量,同样支持线程安全。与第一种实现发放不同的是,每个线程拿到的位置信息视图是一个变化的,并非快照,如果需要快照,通过浅拷贝即可实现。

实现一个线程安全的位置信息类还可以通过内置锁实现,同样,整个MonitorVehicleTracker类还是线程安全的。

上面这个实现通过委托给支持线程安全的内部组件实现线程安全,那么是不是只要内部组件是线程安全的那这个类就是线程安全的呢,显然不是的,如果内部组件的数据存在逻辑关系,或者存在复合操作时,线程安全需要满足既定的逻辑关系,保证符合操作的原子性,这些都是需要额外的同步操作来完成

在扩展原有支持线程安全类的时候,不管是通过继承方式还是组合方式(客户端加锁),都需要保证扩展类中加的锁和基类的锁是一个锁。