java并发copy对象

1

package com.fuyou.concurrency;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import net.jcip.annotations.GuardedBy;
import net.jcip.annotations.ThreadSafe;

/**
* MonitorVehicleTracker
* <p/>
* Monitor-based vehicle tracker implementation
*
* @author Brian Goetz and Tim Peierls
*/
@ThreadSafe
public class MonitorVehicleTracker {
@GuardedBy("this")
private final Map<String, MutablePoint> locations;

public MonitorVehicleTracker(Map<String, MutablePoint> locations) {
this.locations = deepCopy(locations);
}

public synchronized Map<String, MutablePoint> getLocations() {
return deepCopy(locations);
}

public synchronized MutablePoint getLocation(String id) {
MutablePoint loc = locations.get(id);
return loc == null ? null : new MutablePoint(loc);
}

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.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))); //防上会修改point对象

return Collections.unmodifiableMap(result);//
// 实现原是是包装了下map 不支持改变大小的操作
// 仅仅返回的Map不能put remove 操作,
// 但可以对里的对象进行操作
}
}



2
package com.fuyou.concurrency;


import java.awt.Point;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import net.jcip.annotations.ThreadSafe;

/**
* DelegatingVehicleTracker
* 委托模式实现线程安全
* <p/>
* Delegating thread safety to a ConcurrentHashMap
*
* @author Brian Goetz and Tim Peierls
*/
@ThreadSafe
public class DelegatingVehicleTracker {
private final ConcurrentMap<String, Point> locations;
private final Map<String, Point> unmodifiableMap;

public DelegatingVehicleTracker(Map<String, Point> points) {
locations = new ConcurrentHashMap<String, Point>(points);
unmodifiableMap = Collections.unmodifiableMap(locations);
}

public Map<String, Point> getLocations() {
return unmodifiableMap;
}

public Point getLocation(String id) {
return locations.get(id);
}

public void setLocation(String id, int x, int y) {
if (locations.replace(id, new Point(x, y)) == null)
throw new IllegalArgumentException("invalid vehicle name: " + id);
}

// Alternate version of getLocations (Listing 4.8)
public Map<String, Point> getLocationsAsStatic() {
return Collections.unmodifiableMap(
new HashMap<String, Point>(locations));
}
}



package com.fuyou.concurrency;


import net.jcip.annotations.NotThreadSafe;

/**
* MutablePoint
* <p/>
* Mutable Point class similar to java.awt.Point
*
* @author Brian Goetz and Tim Peierls
*/
@NotThreadSafe
public 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版本 是live 的,因为同引用一个map
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值