Single Thread Execution 能通过这座桥的只有一个人
直奔主题, Single Thread Execution也称作Critical Section(临界区),范例如下:
public class SingleThreadGate {
public static void main(String[] args) {
System.out.println("ctrl + c to exit.");
Gate gate = new Gate();
new UserThread(gate, "Alice", "Alaska").start();
new UserThread(gate, "Bobby", "Brazil").start();
new UserThread(gate, "Chris", "Canada").start();
}
}
class Gate{
private int count;
private String name;
private String address;
public void pass(String name, String address){
this.count++;
this.name=name;
this.address=address;
check();
}
public void check() {
if (name.charAt(0) != address.charAt(0)) {
System.out.println("*******BROKEN *******" + toString());
}
}
@Override
public String toString() {
return "No." + count + " " + name + ", " + address;
}
}
class UserThread extends Thread{
private Gate gate;
private String name;
private String address;
public UserThread(Gate gate, String name, String address){
this.gate=gate;
this.name=name;
this.address=address;
}
@Override
public void run() {
System.out.println(name+":begin->");
while(true){
gate.pass(name,address);
Thread.yield();
}
}
}
执行之后,会发现ou一很多broken,因为在执行pass的时候成员变量被其他线程改变了,所以判断name和address不一致,另外toString方法也存在同样的问题,执行过程中 name和address也随时被其他线程改变,所以会导致同一条记录的name和address也不一致,结果如下:
ctrl + c to exit.
Alice:begin->
Chris:begin->
Bobby:begin->
*******BROKEN *******No. Bobby, Brazil
*******BROKEN *******No. Chris, Canada
*******BROKEN *******No. Bobby, Brazil
*******BROKEN *******No. Bobby, Brazil
*******BROKEN *******No. Alice, Canada
*******BROKEN *******No. Chris, Alaska
*******BROKEN *******No. Alice, Canada
*******BROKEN *******No. Bobby, Brazil
*******BROKEN *******No. Chris, Alaska
*******BROKEN *******No. Bobby, Brazil
*******BROKEN *******No. Chris, Alaska
*******BROKEN *******No. Bobby, Brazil
那么如何改进呢?将pass方法加上synchronized关键字,这样就不会有任何错误了。不过这还不能保证整个gate类的所有方法都是安全的,当调用toString方法时还是会有同样的name与address不一致的问题,所以在toString方法上也要加上synchronized关键字,那么check方法还需要么?答案是不需要,因为check方法时私有的,外部无法访问,而且check方法是被pass方法调用的,而pass方法已经同步了,一个类中的synchronized关键之都是锁定的同一对象(this),所以不需要二次锁定,浪费性能。
synchnronized关键字也可以理解成把方法变成原子性操作,在java中,基本数据类型(primitive)如int, char, byte等都是原子性操作的,但是long和double则不一定,所以我们想把long和double这类非原子性类型按照原子性方式操作,需要加上关键字volatile,这样就可以了。
Single Thread Execution 能通过这座桥的只有一个人的更多相关文章
- 多线程系列之二:Single Thread Execution 模式
一,什么是SingleThreadExecution模式?同一时间内只能让一个线程执行处理 二,例子 1.不安全的情况 用程序模拟 三个人频繁地通过一个只允许一个人经过的门.当人通过时,统计人数便会增 ...
- Single Thread Execution设计模式
public class Test { public static void main(String[] args){ // FlightSercurityTest.test(); // EatNoo ...
- 多线程设计模式(一) Single Threaded Execution
这里有一座独木桥.因为桥身非常的细,一次只能允许一个人通过.当这个人没有下桥,另一个人就不能过桥.如果桥上同时又两个人,桥就会因为无法承重而破碎而掉落河里. 这就是Single Threaded Ex ...
- 多线程程序设计学习(2)之single threaded execution pattern
Single Threaded Execution Pattern[独木桥模式] 一:single threaded execution pattern的参与者--->SharedResourc ...
- JAVA并发设计模式学习笔记(二)—— Single Threaded Execution Pattern
注:本文的主要参考资料为结城浩所著<JAVA多线程设计模式>. 单线程执行模式(Single Threaded Execution Pattern)是最简单的多线程设计模式,几乎所有其他的 ...
- 多线程学习之一独木桥模式Single Threaded Execution Pattern
Single Threaded Execution Pattern[独木桥模式] 一:single threaded execution pattern的参与者--->SharedResourc ...
- How does a single thread handle asynchronous code in JavaScript?
原文:https://www.quora.com/How-does-a-single-thread-handle-asynchronous-code-in-JavaScript ----------- ...
- Current thread must be set to single thread apartment (STA) mode before OLE,当前线程不在单线程单元中,因此无法实例化 ActiveX 控件“8856f961-340a-11d0-a96b-00c04fd705a2”。
Add the STAThreadAttribute attribute on the Main method. This attribute is required if your program ...
- C# Current thread must be set to single thread apartment (STA) mode before OLE calls can be made
将箭头指向部分替换为编译器报错的内容即可. 参考文章:https://www.experts-exchange.com/questions/28238490/C-help-needed-Current ...
随机推荐
- JavaScript中的节流和防抖
节流: 在规定时间内,多次触发事件,但是只执行一次 场景:输入框搜索,地图渲染 优化用户体验 /** * 节流 规定时间内不管触发多少次只执行一次 * @param {Function} fn 实际要 ...
- NX二次开发-UFUN新建工程图UF_DRAW_create_drawing
NX9+VS2012 #include <uf.h> #include <uf_draw.h> #include <uf_part.h> UF_initialize ...
- NX二次开发-UFUN所有对象类型的宏定义
/**************************************************************************** Copyright (c) 2010 Sie ...
- spring boot jpa没有自动生成表的原因——加上@Entity
别人的项目弄了好久,竟然是忘记加注解,当然配置文件还是要配置jpa的,pom也要依赖jpa. @Entity jpa: hibernate: ddl-auto: update show-sql: tr ...
- LeetCode 182. Duplicate Emails (查找重复的电子邮箱)
题目标签: 题目给了我们一个 email 的table,让我们找到重复的 email. 可以建立 Person a, Person b, 找到两个表格中,emai 相等 但是 id 不同的 email ...
- hexo next主题深度优化(九),给博客加入主题,护眼主题,护眼色。
文章目录 背景 效果 码 _layout.swig custom.styl eye.js 引用eye.js 直接引用 main.js pjax的函数中重写 个人博客:https://mmmmmm.me ...
- ultis, BIT(x), BITCOUNT(x)
/* http://resnet.uoregon.edu/~gurney_j/jmpc/bitwise.html */ #define BITCOUNT(x) (((BX_(x)+(BX_(x)> ...
- tarjan模板 强联通分量+割点+割边
// https://www.cnblogs.com/stxy-ferryman/p/7779347.html ; struct EDGE { int to, nt; }e[N*N]; int hea ...
- JDBC_数据库连接池DRUID
/** * @Description: TODO(这里用一句话描述这个类的作用) * @Author aikang * @Date 2019/8/26 20:12 */ /* 1.数据库连接池: 1. ...
- 客户端IAP二次验证
1.首先苹果IAP把每次购买抽象成了一个事务(SKPaymentTransaction), - (void)productsRequest:(SKProductsRequest *)request d ...