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的更多相关文章

  1. java_线程、同步、线程池

    线程 Java使用 java.lang.Thread 类代表线程,所有的线程对象都必须是Thread类或其子类的实例 Thread类常用方法 构造方法 public Thread():分配一个新的线程 ...

  2. java_线程-锁

    package com.demo.test3; import java.util.concurrent.CountDownLatch; /** * @author QQ: 1236897 * */ / ...

  3. java_线程

    线程1    与线程相关的概念    线程与进程的区别    线程创建策略    线程组        线程创建策略        并发应用中一般有两种不同的线程创建策略        1直接控制线程 ...

  4. java_线程的几种状态

    java thread的运行周期中, 有几种状态, 在 java.lang.Thread.State 中有详细定义和说明: NEW 状态是指线程刚创建, 尚未启动 RUNNABLE 状态是线程正在正常 ...

  5. java_线程的通信

    线程的通信共有三个方法: wait()运行时阻塞,释放锁 notify()唤醒阻塞线程 notifll()唤醒全部阻塞线程 public class ThreadTest01 { public sta ...

  6. java_线程分类

    线程分为守护线程和用户线程,如java虚拟机的回收机制就是守护线程,线程开始运行它就启动,线程结束它就结束 用户线程变守护线程:Thread(线程).setDaemon(true)

  7. java_线程优先级

    线程优先级分为三个等级: MAX_PIORITY:10  优先 MIN_PRIORITY:1 NORM_PRIORITY:5  默认 getPriority:获取优先级 setPriority:设置优 ...

  8. java_线程类的基本功能

    Thread类是实现了Runnable接口 其方法有: start()开始:开始线程 run()跑:线程内容 currentThread()现在的线程:返回当前线程 getName():获取线程名 s ...

  9. java_线程创建的两种方法

    线程创建的方法有两种: 一 继承Thread类: public class ThreadTest { public static void main(String[] args) { //4)在mai ...

随机推荐

  1. 用Python操作Mysql

    平时的主要编程语言是Java,开发时也主要用Mysql,经常为了测试,调试的目的需要操作数据库,比如备份,插入测试数据,修改测试数据,有些时候不能简单的用SQL就能完成任务,或都很好的完成任务,用Ja ...

  2. bzoj 2555 SubString(SAM+LCT)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2555 [题意] 给定一个字符串,可以随时插入字符串,提供查询s在其中作为连续子串的出现 ...

  3. JAVA与数据库开发(JDBC-ODBC、SQL Server、MySQL)

    1)配置数据库环境和驱动 2)设计数据库结构并创建数据库 3)对数据库进行增删改查操作...

  4. 从Search Sort到Join

    发表于<程序员>2015年4月B的一篇文章,在博客归档下.根据杂志社要求,在自己博客发表该文章亦须注明:本文为CSDN编译整理,未经允许不得转载,如需转载请联系market#csdn.ne ...

  5. leetcode@ [352] Data Stream as Disjoint Intervals (Binary Search & TreeSet)

    https://leetcode.com/problems/data-stream-as-disjoint-intervals/ Given a data stream input of non-ne ...

  6. 将android Settings 源码 导入到 eclipse工程

    1.  新建 android 项目 拷贝源码/packages/apps/Settings到你的其它目录. 在eclipse中,新建项目,但是要从exitting source选择: 2. 导入相关的 ...

  7. Linux管线命令

    一.什么是管线命令 bash 命令运行的时候有输出的数据会出现! 那么如果这群数据必需要经过几道手续之后才能得到我们所想要的格式,应该如何来配置? 这就牵涉到管线命令的问题了 (pipe) ,管线命令 ...

  8. Struts2通配符映射

    1.一个Web 应用可能有成百上千个 action 声明. 可以利用 struts 提供的通配符映射机制把多个彼此相似的映射关系简化为一个映射关系 2.通配符映射规则 –若找到多个匹配, 没有通配符的 ...

  9. poj 1915 http://poj.org/problem?id=1915

    /**< */#include <stdio.h> #include <string.h> #include <stdlib.h> #include < ...

  10. codeforces Ebony and Ivory(水题)

    A. Ebony and Ivory time limit per test 2 seconds memory limit per test 256 megabytes input standard ...