Java并发 - 线程安全类探索

1.简单设置线程安全类

设计车辆追踪器,获取车辆位置和更新车辆位置信息(坐标x,y)展示显示化大屏

版本一

  • 非线程安全车辆对象【不可变】(MutablePoint)
  • 线程安全车辆容器
// 非线程安全
public class MutablePoint {
    public int x, y;

    public MutablePoint() {
        this.x = 0;
        this.y = 0;
    }

    public MutablePoint(MutablePoint point) {
        this.x = point.x;
        this.y = point.y;
    }
}
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class MonitorVehicleTracker {
    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 getLocations(String id) {
        MutablePoint mutablePoint = locations.get(id);
        return mutablePoint == null ? null : new MutablePoint(mutablePoint);
    }

    // 更新车辆的位置
    public synchronized void setLocations(String id, int x, int y) {
        MutablePoint mutablePoint = locations.get(id);
        if (null == mutablePoint) {
            throw new IllegalArgumentException("No such ID :" + id);
        }
        mutablePoint.x = x;
        mutablePoint.y = y;
    }

    // 深度复制
    private static Map<String, MutablePoint> deepCopy(Map<String, MutablePoint> m) {
        Map<String, MutablePoint> result = new HashMap<>();
        for (String id : m.keySet()) {
            result.put(id, new MutablePoint(m.get(id)));
        }
        // 创建一个不可变,不可修改的Map
        return Collections.unmodifiableMap(result);
    }
}

版本优缺点

  • 优点
    • getLocations可保证数据一致性
  • 缺点
    • 使用deepCopy方式保证线程安全,对象的大量创建会导致内存不足
    • getLocations时获取的车辆信息不是最新车辆信息

getLocations分析:

在getLocations和setLocations使用sync同步字段,在导出数据时,若locations对象数据很大,此时其他线程调用了setLocations时便会阻塞住,则当前线程导出的数据与用户查看的数据一致(数据一致性)。但数据没有发生更新。

版本二

  • 线程安全车辆对象【不可变】(Point)
  • 线程安全车辆容器(DelegatingVehicleTracker)
// 使用了final作用域,对象状态线程安全,“不可变性”
public class Point {
    public final int x,y;

    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
}
//使用线程安全容器concurrentHashMap保证线程安全
public class DelegatingVehicleTracker {
    // 另外一种线程安全的方式CopyOnWriteArrayList
    private final ConcurrentMap<String, Point> locations;
    private final Map<String, Point> unmodiflableMap;

    public DelegatingVehicleTracker(Map<String, Point> points) {
        locations = new ConcurrentHashMap<>(points);
        unmodiflableMap = Collections.unmodifiableMap(locations);
    }
    
    // 返回的车辆信息拥有当前线程的数据一致性。线程A导出,线程B更新车辆位置的时候,线程A导出的数据还是他之前获取的数据。
    public Map<String,Point> getLocationsNotChange(){
        return Collections.unmodifiableMap(new HashMap<>(locations));
    }

    // 获取的数据是及时发生更改的,返回的是车辆的快照
    public Map<String, Point> getLocations() {
        return locations;
    }

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

    public void setLocations(String id, int x, int y) {
        // 替换key中的值
        if (locations.replace(id, new Point(x, y)) == null) {
            throw new IllegalArgumentException("invalid vehicle name:" + id);
        }
    }
}

仔细观察上述版本一和版本二中getLocations及保存车辆的容器,容器如何保证线程安全,及getLocations如何保证数据一致性与保证获取的是最新数据。

版本三

  • 线程安全车辆对象【可变】(SafePoint )
  • 线程安全车辆容器(PublishingVehicleTracker )
public class SafePoint {
    private int x, y;

    public SafePoint(int[] a) {
        this(a[0], a[1]);
    }

    public SafePoint(SafePoint p) {
        this.x = p.x;
        this.y = p.y;
    }

    // 使用对象锁
    public synchronized int[] get() {
        return new int[]{x, y};
    }

    public SafePoint(int x, int y) {
        this.x = x;
        this.y = y;
    }
	// 使用对象锁
    public synchronized void set(int x, int y) {
        this.x = x;
        this.y = y;
    }
}
// 可发布
public class PublishingVehicleTracker {
    private final Map<String, SafePoint> locations;

    private final Map<String, SafePoint> umodifiableMap;

    // TODO 把线程安全委托给ConcurrentHashMap
    public PublishingVehicleTracker(Map<String, SafePoint> locations) {
        this.locations = new ConcurrentHashMap<>(locations);
        this.umodifiableMap = Collections.unmodifiableMap(this.locations);
    }

    public Map<String, SafePoint> getLocations() {
        return this.umodifiableMap;
    }

    public SafePoint getLocations(String id) {
        return locations.get(id);
    }

    public void setLocations(String id, int x, int y) {
        if (!locations.containsKey(id)) {
            throw new IllegalArgumentException("invalid vehicle name :" + id);
        }
        // TODO locations.get 和 set 都是竞争同一个锁的。这样子才能保证线程安全。如果x,y 分别设置一个set和get则导致x和y中出现修改了x,而y还没有更改
        locations.get(id).set(x, y);	
    }
}

观察上述版本二和版本三中如何保证车辆信息对象在可变条件下线程安全。

2.对现在有的线程安全类添加功能小探索。

  • 代码复用
  • 开发成本及维护成本(原有的代码已经测试过)

假设需要一个线程安全链表,他提供一个原子的“若没有则添加(Put-If-Absent)” 同步的List已实现了大部分功能,我们可以根据他提供的contains和add方法来构造一个“若没有则添加”的操作。

实现”若没有则添加“的概念很简单:先检查再执行。先检查这个元素是否存在,不存在则进行添加

  1. 修改原始类(通常无法做到)

  2. 扩展类(并非所有类的状态都向子类公开,大部分不适合)

    public class BetterVector<E> extends Vector<E>{
        public synchronized boolean putIfAbsent(E x){
            boolean absent = !contains(x);
            if(absent)
                add(x);
            return absent;
        }
    }
    
  3. 客户端加锁(非线程安全)

    public class ListHelper<E>{
        public List<E> list = Collections.synchronized(new ArrayList<E>());
        // 无效加锁 ListHelper锁假象。list 跟 ListHelper 是两个对象
        public synchronized boolean putIfAbsent(E x){
             boolean absent = !list.contains(x);
            if(absent)
                add(x);
            return absent;
        }
    }
    
  4. 客户端加锁(线程安全)

    public class ListHelper<E>{
        public List<E> list = Collections.synchronized(new ArrayList<E>());
        public boolean putIfAbsent(E x){
            synchronized(list){
                boolean absent = !list.contains(x);
                if(absent)
                    add(x);
                return absent;
            }
        }
    }
    
  5. 组合(用户只能通过ImprovedList 访问)

    public class ImprovedList<T> implements List<T> {
        private final List<T> list;
        public ImprovedList(List<T> list){
            this.list = list;
        }
        public synchronized boolean putIfAbsent(T x) {
            boolean absent = !list.contains(x);
            if(absent)
                add(x);
            return absent;
        }
        public synchronized void clear(){
            list.clear();
        }
        // ... 按照类似的方式委托List的其他方法
    }
    

你可能感兴趣的:(Java,java,python,开发语言)