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 ...
随机推荐
- opencv行人检测里遇到的setSVMDetector()问题
参考了博客http://blog.csdn.net/carson2005/article/details/7841443 后,自己动手后发现了一些问题,博客里提到的一些问题没有解决 ,是关于为什么图像 ...
- salt 批量部署与配置
salt是啥? salt是一个大型分布式的配置管理系统(安装升级卸载软件,检测环境),也是一个远程命令执行系统. salt 分为 master和minion,master顾名思义就是老大,管理子节点: ...
- POJ 2280&&hdu 1661
题意:给定平面上的N个点,属性分别标记为0和1,然后找一条直线,直线上的点全部溶解,一侧的1溶解,另一侧的0溶解.求出最多能溶解的点的个数. 思路:暴力枚举每个点,扫描线旋转.先做优化,如果一侧溶解0 ...
- (转载)OC学习篇之---第一个程序HelloWorld
之前的一片文章简单的介绍了OC的相关概述,从这篇开始我们就开始学习OC的相关知识了,在学习之前,个人感觉需要了解的其他的两门语言:一个是C/C++,一个是面向对象的语言(当然C++就是面向对象,不过这 ...
- 【更新sql server数据项的长度】////【复制数据到另一张表中】
由于设计时没考虑周全,之后发现长度不够,手动修改又不可以... 重新新建也不行啊>>>>>>>>>里面的数据怎么办 so:直接用代码了.... a ...
- 获取week of year的小程序
#coding=utf8 import urllib, BeautifulSoup web=urllib.urlopen("http://whatweekisit.com/") s ...
- http和数据库sql分析与窃听技术
用tunnel,tunnel是一种技术称谓,将其放到真正的服务器和客户端之间.调试阶段可以使用webcream运行tomcat作为模拟的真正的服务器. 具体:用apache axis及其项目中的工具t ...
- gulp之静态资源防缓存处理
最近,因为校友网项目开始有些规模了.开始就要考虑对静态资源进行工程自动化的管理.一讲到前端的自动化工具,大家或许都会想到Grunt,Gulp,或者百度的FIS.这三个都有各自的特点,大家可以依据自己的 ...
- How to include JavaScript file in JSF
In JSF 2.0, you can use <h:outputScript /> tag to render a HTML "script" element, an ...
- poj 3180 The Cow Prom(强联通分量)
http://poj.org/problem?id=3180 The Cow Prom Time Limit: 1000MS Memory Limit: 65536K Total Submissi ...