首页 / JAVA / java_线程安全-service
java_线程安全-service
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java_线程安全-service,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4862字,纯文字阅读大概需要7分钟。
内容图文
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 stubreturn "x: "+x+", y: "+y; } } //返回locations后 修改point坐标,对返回map里的point->有影响class DelegateTracker { privatefinal ConcurrentMap<String, Point> locations; privatefinal 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); } publicvoid setLocation(String id, int x, int y) { if (locations.replace(id, new Point(x, y)) == null) { thrownew IllegalArgumentException("invalid id - " + id); } } } //返回locations后 修改point坐标,对返回map里的point -> 没有影响class DelegateTrackerFixedPoint { privatefinal ConcurrentMap<String, Point> locations; privatefinal 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); } publicvoid setLocation(String id, int x, int y) { if (locations.replace(id, new Point(x, y)) == null) { thrownew IllegalArgumentException("invalid id - " + id); } } } publicclass ThreadDemo1 { publicstaticvoid 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; } publicsynchronizedint[] get() { returnnewint[] { x, y }; } publicsynchronizedvoid set(int x, int y) { this.x = x; this.y = y; } } //取出 Locations后,如果更改点位置,locations里的点会跟随变化class PublishTracker{ privatefinal ConcurrentMap<String, SafePoint> locations; privatefinal 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); } publicvoid setLocation(String id, int x, int y) { if (!locations.containsKey(id)) { thrownew IllegalArgumentException("invalid id - " + id); } locations.get(id).set(x, y); } } publicclass ThreadTest2 { /** * @param args */publicstaticvoid main(String[] args) { // TODO Auto-generated method stub } }
原文:http://www.cnblogs.com/MarchThree/p/4769851.html
内容总结
以上是互联网集市为您收集整理的java_线程安全-service全部内容,希望文章能够帮你解决java_线程安全-service所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。