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 ...
随机推荐
- STM32F407 外扩SRAM
字节控制功能.支持高/低字节控制. 看看实现 IS62WV51216 的访问,需要对 FSMC进行哪些配置. 这里就做一个概括性的讲解.步骤如下: 1)使能 FSMC 时钟,并配置 FSMC 相关的 ...
- JS数组(Array)操作汇总
1.去掉重复的数组元素.2.获取一个数组中的重复项.3.求一个字符串的字节长度,一个英文字符占用一个字节,一个中文字符占用两个字节.4.判断一个字符串中出现次数最多的字符,统计这个次数.5.数组排序. ...
- cocos2d-x 不能在android真机debug的问题
最近在做cocos2d-x开发的时候,发现在android真机上不能调试C++代码,显示如下警告信息 Ignoring packet error, continuing... warning: unr ...
- aggregation(2):adaptive Boosting (AdaBoost)
给你这些水果图片,告诉你哪些是苹果.那么现在,让你总结一下哪些是苹果? 1)苹果都是圆的.我们发现,有些苹果不是圆的.有些水果是圆的但不是苹果, 2)其中到这些违反"苹果都是圆的" ...
- ASP.NET MVC3 系列教程 – Web Pages 1.0
http://www.cnblogs.com/highend/archive/2011/04/14/aspnet_mvc3_web_pages.html I:Web Pages 1.0中以“_”开头的 ...
- 有关android UI 线程
1. GUI线程框架 常见的 Swing, SWT框架都是作为单线程子系统来实现的,实际上不仅限于在Java中, Qt.MacOS Cocoa以及其他的环境中的GUI框架都是单线程的.虽然很多人尝试过 ...
- sublime text 2使用经验
1. Package Control 安装代码: import urllib2,os; pf='Package Control.sublime-package'; ipp=sublime.instal ...
- 终于弄好了 homework-09
近一周的时间,顶着编译大作业严重搁置的压力,天天搞,终于把网页动态展示的搞出来了!恩,还挺好看~ 因为是最后一次作业了,也是因为天生的完美主义强迫症,做到自己满意才放心停下来.不过,这个过程,看着同学 ...
- ASP.NET中身份验证的三种方法
Asp.net的身份验证有有三种,分别是"Windows | Forms | Passport",其中又以Forms验证用的最多,也最灵活.Forms 验证方式对基于用户的验证授权 ...
- IIS6的SSL配置,如何配置SSL到登陆页,如何将SSL证书设置成受信任的证书
一. 申请证书1. 到受信任的机构申请 略 2. 到自建的证书服务器申请 a. 安装证书服务 通过控制面板中的“添加/删除程序”,选择“添加/删除Windows组件”.在Windows组件向导中找到“ ...