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 ...
随机推荐
- 用jQuery基础
要使用jQuery要引用jQuery文件,在头标签中引用 1 <script src="jquery-1.11.2.min.js"></script> ...
- JavaWeb学习篇之----web应用的虚拟目录映射和主机搭建(Tomcat)
从今天开始来学习JavaWeb的相关知识,之前弄过一段时间JavaWeb的,就是在做毕业设计的时候搞过,但是那时候完全是为了任务去学习,所以效果不好,好多东西都没有深入的研究过,所以接下来的一段时间我 ...
- eclipse如何实现智能提示功能
一直时候用idea很久没有使用eclipse了,idead的ctrl+鼠标滚轮是可以调节字体大小,这项功能是我的最爱. 早就忘记eclipse的智能助手设置,今天翻下以前的笔记,觉得还是做个博客方便今 ...
- Java-Class-I:java.util.List
ylbtech-Java-Class-I:java.util.List 1.返回顶部 1.1.import java.util.ArrayList;import java.util.List; 1.2 ...
- ionic-CSS:ionic 网格(Grid)
ylbtech-ionic-CSS:ionic 网格(Grid) 1.返回顶部 1. ionic 网格(Grid) ionic 的网格(Grid)和其他大部分框架有所不同,它采用了弹性盒子模型(Fle ...
- spring boot部署到阿里云碰到的总总问题
2375错误,我没装docker,从pom中删了吧 mysql,不能写本机对外,得写127.0.0.1. 如何生成jar包,在pom中写上jar <groupId>com.coding&l ...
- flutter SnackBar
无法弹出 snackbar时
- 创建 Angular 8.0 项目
创建 Angular 8.0 项目,首先确保已经安装了 nodejs,如果没有安装,请看这篇文章安装:node.js 安装 1.新建一个空文件夹 angularproject,作为工作区 2.安装 A ...
- Python3 From Zero——{最初的意识:005~文件和I/O}
一.输出重定向到文件 >>> with open('/home/f/py_script/passwd', 'rt+') as f1: ... print('Hello Dog!', ...
- HDU 3607 线段树+DP+离散化
题意:从左往右跳箱子,每个箱子有金币数量,只能从矮处向高处跳,求最大可获得金币数,数据规模1<=n<=1e5. 显然是一个dp的问题,不难得出dp[ i ] = max(dp[j] )+v ...