java_线程安全-service
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 } }
java_线程安全-service的更多相关文章
- java_线程、同步、线程池
线程 Java使用 java.lang.Thread 类代表线程,所有的线程对象都必须是Thread类或其子类的实例 Thread类常用方法 构造方法 public Thread():分配一个新的线程 ...
- java_线程-锁
package com.demo.test3; import java.util.concurrent.CountDownLatch; /** * @author QQ: 1236897 * */ / ...
- java_线程
线程1 与线程相关的概念 线程与进程的区别 线程创建策略 线程组 线程创建策略 并发应用中一般有两种不同的线程创建策略 1直接控制线程 ...
- java_线程的几种状态
java thread的运行周期中, 有几种状态, 在 java.lang.Thread.State 中有详细定义和说明: NEW 状态是指线程刚创建, 尚未启动 RUNNABLE 状态是线程正在正常 ...
- java_线程的通信
线程的通信共有三个方法: wait()运行时阻塞,释放锁 notify()唤醒阻塞线程 notifll()唤醒全部阻塞线程 public class ThreadTest01 { public sta ...
- java_线程分类
线程分为守护线程和用户线程,如java虚拟机的回收机制就是守护线程,线程开始运行它就启动,线程结束它就结束 用户线程变守护线程:Thread(线程).setDaemon(true)
- java_线程优先级
线程优先级分为三个等级: MAX_PIORITY:10 优先 MIN_PRIORITY:1 NORM_PRIORITY:5 默认 getPriority:获取优先级 setPriority:设置优 ...
- java_线程类的基本功能
Thread类是实现了Runnable接口 其方法有: start()开始:开始线程 run()跑:线程内容 currentThread()现在的线程:返回当前线程 getName():获取线程名 s ...
- java_线程创建的两种方法
线程创建的方法有两种: 一 继承Thread类: public class ThreadTest { public static void main(String[] args) { //4)在mai ...
随机推荐
- Tomcat中的线程池StandardThreadExecutor
之所以今天讨论它,因为在motan的的NettyServer中利用它这个线程池可以作为业务线程池,它定制了一个自己的线程池.当然还是基于jdk中的ThreadExecutor中的构造方法和execut ...
- aggregation(2):adaptive Boosting (AdaBoost)
给你这些水果图片,告诉你哪些是苹果.那么现在,让你总结一下哪些是苹果? 1)苹果都是圆的.我们发现,有些苹果不是圆的.有些水果是圆的但不是苹果, 2)其中到这些违反"苹果都是圆的" ...
- 用VMware 8安装Ubuntu 12.04详细过程(图解)
转载 http://www.cnblogs.com/achillesyang/archive/2012/06/21/2557152.html
- Java Client for Google Cloud Storage
关于Google Cloud Storage Google Cloud Storage有益于大文件的存储与服务(serve).此外,Cloud Storage提供了对访问控制列表(ACLs)的使用,提 ...
- [现代程序设计]homework-03
Homework-03 队员: 11061193 薛亚杰 11061192 周敏轩 11061190 李孟 材料阅读 我们三个人将以下材料仔细阅读,觉得十分受益.下面是我们的总结和分享: 1)代 ...
- Java缓存学习之三:CDN缓存机制
CDN是什么? 关于CDN是什么,此前网友详细介绍过. CDN是Content Delivery Network的简称,即"内容分发网络"的意思.一般我们所说的CDN加速,一般是指 ...
- SpringMVC学习笔记
1.严格实现MVC设计思想的框架,严格分层,减少耦合: 2.组件(红色必需) 2.1 DispatcherServlet 前端控制器 2.2 Controller 业务控制器 2.3 Handler ...
- 【转】iOS 硬件授权检测:定位服务、通讯录、日历、提醒事项、照片、蓝牙共享、麦克风、相机等
iOS系统版本的不断升级的前提,伴随着用户使用设备的安全性提升,iOS系统对于App需要使用的硬件限制也越来越严格,App处理稍有不妥,轻则造成功能不可用用户还不知道,重则会造成App Crash. ...
- 斜率DP题目
uva 12524 题意:沿河有n个点,每个点有w的东西,有一艘船从起点出发,沿途可以装运东西和卸载东西,船的容量无限,每次把wi的东西从x运到y的花费为(y-x)*wi; 问把n个点的东西合并成k个 ...
- Spring入门(5)-自动装配Bean属性
Spring入门(5)-自动装配Bean属性 本文介绍如何装配Bean属性. 0. 目录 ByName ByType constructor 默认自动装配 混合使用自动装配和显示装配 1. ByNam ...