package com.demo.test; import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; /** * @author QQ: 1236897 * */ //基于委托的线程安全 class Point { public final int x, y; public Point(int x, int y) { this.x = x; this.y = y; } /* (non-Javadoc) * @see java.lang.Object#toString() */ @Override public String toString() { // TODO Auto-generated method stub return "x: "+x+", y: "+y; } } //返回locations后 修改point坐标,对返回map里的point->有影响 class DelegateTracker { private final ConcurrentMap<String, Point> locations; private final Map<String, Point> unmodifiableMap; public DelegateTracker(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 id - " + id); } } } //返回locations后 修改point坐标,对返回map里的point -> 没有影响 class DelegateTrackerFixedPoint { private final ConcurrentMap<String, Point> locations; private final Map<String, Point> unmodifiableMap; public DelegateTrackerFixedPoint(Map<String, Point> points) { locations = new ConcurrentHashMap<String, Point>(points); unmodifiableMap = Collections.unmodifiableMap(locations); } //不同 public Map<String, Point> getLocations() { return Collections.unmodifiableMap(new HashMap<String,Point>(locations)); } 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 id - " + id); } } } public class ThreadDemo1 { public static void main(String[] args) { //test1 - DelegateTracker Point p1 = new Point(1,1); Point p2 = new Point(2,2); Point p3 = new Point(3,3); Point p4 = new Point(4,4); Point p5 = new Point(5,5); Map<String,Point> points = new HashMap<String,Point>(); points.put("1", p1); points.put("2", p2); points.put("3", p3); points.put("4", p4); points.put("5", p5); DelegateTracker delegateTracker = new DelegateTracker(points); delegateTracker.setLocation("2", 99, 99); Map<String,Point> result = delegateTracker.getLocations(); delegateTracker.setLocation("3", 33, 33); for(String key:result.keySet()) { Point point = result.get(key); System.out.println(point.toString()); } System.out.println("-----------------------------------------"); //test2 - DelegateTrackerFixedPoint Point f1 = new Point(1,1); Point f2 = new Point(2,2); Point f3 = new Point(3,3); Point f4 = new Point(4,4); Point f5 = new Point(5,5); Map<String,Point> points2 = new HashMap<String,Point>(); points2.put("1", f1); points2.put("2", f2); points2.put("3", f3); points2.put("4", f4); points2.put("5", f5); DelegateTrackerFixedPoint delegateTrackerFixedPoint = new DelegateTrackerFixedPoint(points2); delegateTrackerFixedPoint.setLocation("2", 99, 99); Map<String,Point> result2 = delegateTrackerFixedPoint.getLocations(); delegateTrackerFixedPoint.setLocation("3", 33, 33); for(String key:result2.keySet()) { Point point = result2.get(key); System.out.println(point.toString()); } } }
package com.demo.test2; import java.util.Collections; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; /** * @author QQ: 1236897 * */ // 基于发布的线程安全 操作对象安全 class SafePoint { private int x, y; private SafePoint(int[] a) { this(a[0], a[1]); } public SafePoint(SafePoint p) { this(p.get()); } public SafePoint(int x, int y) { this.x = x; this.y = y; } public synchronized int[] get() { return new int[] { x, y }; } public synchronized void set(int x, int y) { this.x = x; this.y = y; } } //取出 Locations后,如果更改点位置,locations里的点会跟随变化 class PublishTracker{ private final ConcurrentMap<String, SafePoint> locations; private final Map<String, SafePoint> unmodifiableMap; public PublishTracker(Map<String, SafePoint> points) { locations = new ConcurrentHashMap<String, SafePoint>(points); unmodifiableMap = Collections.unmodifiableMap(locations); } public Map<String, SafePoint> getLocations() { return unmodifiableMap; } public SafePoint getLocation(String id) { return locations.get(id); } public void setLocation(String id, int x, int y) { if (!locations.containsKey(id)) { throw new IllegalArgumentException("invalid id - " + id); } locations.get(id).set(x, y); } } public class ThreadTest2 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub } }