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 ...
随机推荐
- 使用C语言实现二维,三维绘图算法(1)-透视投影
使用C语言实现二维,三维绘图算法(1)-透视投影 ---- 引言---- 每次使用OpenGL或DirectX写三维程序的时候, 都有一种隔靴搔痒的感觉, 对于内部的三维算法的实现不甚了解. 其实想想 ...
- C语言中 v...printf类函数的用法
C语言的自学渐渐接近尾声,今天学到了标准库中的stdarg.h头,里面关联了stdio.h头里面的一类函数:v...printf函数,里面举的例子看了之后还是不太明白,google了一下依旧不是很懂, ...
- asp.net MVC 中@Html.Partial,@Html.Action,@Html.RenderPartial,@Html.RenderAction区别
@Html.Action:需要有对应的Action,并且Action方法有返回值.(注:处理完业务逻辑同时,也需要返回所需值) @{Html.RenderAction}:需要有对应的Action,Ac ...
- 写i2c_client驱动的两种方式
http://www.cnblogs.com/simonshi/archive/2011/02/24/1963426.html
- SVM核函数与软间隔
核函数 在上文中我们已经了解到使用SVM处理线性可分的数据,而对于非线性数据需要引入核函数的概念它通过将数据映射到高维空间来实现线性可分.在线性不可分的情况下,支持向量机通过某种事先选择的非线性映射( ...
- Tkinter教程之Scrollbar篇
本文转载自:http://blog.csdn.net/jcodeer/article/details/1811319 '''Tkinter教程之Scrollbar篇'''#Scrollbar(滚动条) ...
- Cloudera CDH5 部署实战指南(离线安装)
配置软件源服务器 1.安装createreporpm -ivh deltarpm-3.5-0.5.20090913git.el6.x86_64.rpm rpm -ivh python-deltarpm ...
- J2SE7规范_2013.2_类型_命名
3.1 字面量:包括整型,实型,字符,字符串,布尔,null 整形: 除非后面有个l或L,一般总是int类型 除非是0x,0,0b开头,一般总是十进制 无论什么进制,中间都可以有_,无意义,只是看 ...
- 《Genesis-3D开源游戏引擎-官方录制系列视频教程:基础操作篇》
注:本系列教程仅针对引擎编辑器:v1.2.2及以下版本 G3D基础操作 第一课<G3D编辑器初探> G3D编辑器介绍,依托于一个复杂场景,讲解了场景视图及其基本操作,属性面板和工具栏的 ...
- 《Genesis-3D开源游戏引擎完整实例教程-2D射击游戏篇05:角色中弹》
5.角色中弹 概述: 为了使游戏具有挑战性,大部分游戏设定中,游戏角色都有生命限制.即在游戏中,由于玩家的操控操控不当,导致游戏角色死亡游戏终止.打飞机游戏也不例外,当敌人击中角色的时候,角色宣判死亡 ...