多线程操作-嘱托线程安全
多线程操作---委托线程安全
1.使用复制来保证线程安全
2.缩小同步范围
1.使用复制来保证线程安全
class MonitorVehicleTracker{ private final Map<String,MutablePoint> locations; public MonitorVehicleTracker(Map<String,MutablePoint> locations){ this.locations = deepCopy(locations); } public synchronized Map<String,MutablePoint> getLocations(){ //我拥有的是外界传给我的拷贝,所以外界的locations改变了也不会影响我 return deepCopy(locations); } public synchronized MutablePoint getLocation(String id){ MutablePoint loc = locations.get(id); //传出去的也是一份拷贝,也意味着通过这个方法,外界只能看locations里面的数据,而不能改 return loc==null?null:new MutablePoint(loc); } public synchronized void setLocation(String id,int x,int y){ //只能通过这个接口才能改location数据,保证了修改的可控性 MutablePoint loc = locations.get(id); if(loc==null) throw new IllegalArgumentException("No such ID:"+id); loc.x=x; loc.y=y; } private static Map<String ,MutablePoint> deepCopy(Map<String,MutablePoint> m){ Map<String ,MutablePoint> result = new HashMap<String,MutablePoint>(); for(String id:m.keySet()) result.put(id,new MutablePoint(m.get(id))); return Collections.unmodifiableMap(result);//这个map不允许增加或者减少元素 } } class MutablePoint{ public int x,y; public MutablePoint(){ x=0;y=0; } public MutablePoint(MutablePoint p){ this.x=p.x; this.y=p.y; } }
2.缩小同步范围
class MonitorVehicleTracker{ private final Map<String,MutablePoint> locations; private final Map<String,MutablePoint> unmodifiableMap; public MonitorVehicleTracker(Map<String,MutablePoint> locations){ this.locations = new ConcurrentHashMap<String,MutablePoint>(locations); this.unmodifiableMap = Collections.unmodifiableMap(locations); } public /*synchronized*/ Map<String,MutablePoint> getLocations(){ return this.unmodifiableMap; } public /*synchronized*/ MutablePoint getLocation(String id){ return locations.get(id); } public /*synchronized*/ void setLocation(String id,int x,int y){ MutablePoint loc = locations.get(id); if(loc==null) throw new IllegalArgumentException("No such ID:"+id); loc.set(x, y); //只有这一小段同步了 } private static Map<String ,MutablePoint> deepCopy(Map<String,MutablePoint> m){ Map<String ,MutablePoint> result = new HashMap<String,MutablePoint>(); for(String id:m.keySet()) result.put(id,new MutablePoint(m.get(id))); return Collections.unmodifiableMap(result);//这个map不允许增加或者减少元素 } } class MutablePoint{ public int x,y; public MutablePoint(){ x=0;y=0; } public MutablePoint(MutablePoint p){ this.x=p.x; this.y=p.y; } public synchronized void set(int x,int y){ this.x=x; this.y=y; } }