多线程操作---委托线程安全

1.使用复制来保证线程安全
[code]

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;
}
}
[/code]

2.缩小同步范围
[code]
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;
}
}
[/code]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值