多线程程序设计学习(2)之single threaded execution pattern
Single Threaded Execution Pattern【独木桥模式】
一:single threaded execution pattern的参与者
--->SharedResource(共享资源)
二:single threaded execution pattern模式什么时候使用
--->多线程程序设计时
--->数据可被多个线程访问的时候
--->共享资源状态可能变化的时候
--->需要确保数据安全性的时候
三:single threaded execution pattern思考
--->synchronized一见到它,势必保护着什么公共资源的数据。保证数据安全,就得所有该保护的地方都得保护。
--->保护公共资源的数据的范围叫临界区,临界区尽可能的小。提高性能。
--->程序设计的时候,一定要防止死锁的发生。主要是同步方法的外部调用顺序,防止交叉调用,多线程时,会发生死锁。
案例:三个人来回通过一扇门,通过时记录该人的姓名和地址。
门类(公共资源)
package com.yeepay.sxf.thread1;
/**
* 门类(代表着多线程程序访问的公共资源)
* @author sxf
*
*/
public class Gate {
//计数器
private int counter=0;
//通过这扇门的人的名字
private String name;
//正在通过这扇门的人的地址
private String address;
//通过这扇门的动作
//存在多线程同时访问该资源。(临界区需要做同步)
public synchronized void passGate(String name,String address){
counter+=1;
this.name=name;
this.address=address;
check();
}
//记录通过这扇门的人的信息
@Override
public String toString() { return "NO:"+counter+"人 name="+name+" address="+address;
} //检查,如果数据不完整,说明多线程程序的安全性挂掉。打印报警信息
private void check(){
if(name.charAt(0)!=address.charAt(0)){
System.out.println("**********breaken*******"+toString());
}
}
}
人类(线程类)
package com.yeepay.sxf.thread1;
/**
* 人类(不同的人代表不同的线程,访问公共资源门)
* @author sxf
*
*/
public class UserThread implements Runnable {
//门
private final Gate gate;
//当前的人名
private final String myName;
//当前的人的地址
private final String myAddress;
//线程的构造器
public UserThread(Gate gate,String myName,String myAddress) {
this.gate=gate;
this.myName=myName;
this.myAddress=myAddress;
} //线程体
@Override
public void run() {
System.out.println("UserThread.run() begin:"+myName);
while (true) {
gate.passGate(myName, myAddress);
} } }
测试类(主线程)
package com.yeepay.sxf.thread1;
/**
* 测试类
* @author sxf
*
*/
public class Test {
public static void main(String[] args) {
//先声明一个门
Gate gate=new Gate(); //声明三个线程
Thread user1Thread=new Thread(new UserThread(gate, "Asxf", "Ahenan"));
Thread user2Thread=new Thread(new UserThread(gate,"Bsxs","Bhenan"));
Thread user3Thread=new Thread(new UserThread(gate,"Csxy","Chenan")); //启动三个线程
user1Thread.start();
user2Thread.start();
user3Thread.start();
}
}
多线程程序设计学习(2)之single threaded execution pattern的更多相关文章
- 多线程学习之一独木桥模式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
这里有一座独木桥.因为桥身非常的细,一次只能允许一个人通过.当这个人没有下桥,另一个人就不能过桥.如果桥上同时又两个人,桥就会因为无法承重而破碎而掉落河里. 这就是Single Threaded Ex ...
- 多线程程序设计学习(13)Active Object pattern
Active Object[接收异步消息的对象] 一:Active Object的参与者--->客户端线程(发起某种操作请求处理)--->代理角色(工头)--->实际执行者(工人)- ...
- 多线程程序设计学习(12)Thread-soecific storage pattern
Thread-Specific-Storage[线程保管箱] 一:Thread-Specific Storage的参与者--->记录日志的线程(ClientThread)--->负责获取不 ...
- 多线程程序设计学习(7)read-write lock pattern
Read-Write Lock Pattern[读写]一:Read-Write Lock Pattern的参与者--->读写锁--->数据(共享资源)--->读线程--->写线 ...
- 多线程系列之二:Single Thread Execution 模式
一,什么是SingleThreadExecution模式?同一时间内只能让一个线程执行处理 二,例子 1.不安全的情况 用程序模拟 三个人频繁地通过一个只允许一个人经过的门.当人通过时,统计人数便会增 ...
- 多线程程序设计学习(3)immutable pattern模式
Immutable pattern[坚不可摧模式] 一:immutable pattern的参与者--->immutable(不变的)参与者 1.1:immutable参与者是一个 ...
- 多线程程序设计学习(6)Producer-Consumer模式
Producer-Consumer[生产消费者模式]一:Producer-Consumer pattern的参与者--->产品(蛋糕)--->通道(传递蛋糕的桌子)--->生产者线程 ...
随机推荐
- C#textbox右击弹出菜单
给窗口体拖一个contextMenuTrip 控件,也就是右键菜单控件,这时你就不要给这个控件写内容了, 选中textBox 然后点属性窗口,把它的contextMenuTrip 属性选中你刚才托的那 ...
- [原]android不支持命名的semaphore
之前sem_open在iOS上, 创建命名的semaphore没有问题 (iOS不支持匿名的semaphore), 但是现在Android平台的sem_open时候报错,返回ENOSYS. 命名的se ...
- Codeforces Round #241 (Div. 2)->B. Art Union
B. Art Union time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- libcurl编程学习
一.curl简介 curl是一个利用URL语法在命令行方式下工作的文件传输工具.它支持的协议有:FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE 以 ...
- Java中super的用法并与this的区别(转载)
一.子类中如果需要调用父类中的构造函数,则需要使用super(),且必须在构造函数中的第一行 public class Demo1 { public static void main(String[] ...
- 轻轻修改配置文件完成 OpenStack 监控
当我们使用虚拟化云平台 OpenStack 时,必然要时时监控其虚拟机性能,随着近年企业级数据中心的不断发展,像混合虚拟化环境的业务需求也在持续增长中,因而也随之带来的监控需求更显重要,所以小编带来一 ...
- Oracle的学习二:表管理(数据类型、创建/修改表、添加/修改/删除数据、数据查询)
1.Oracle表的管理 表名和列名的命名规则: 必须以字母开头: 长度不能超过30个字符: 不能使用oracle的保留字: 只能使用如下字符:A-Z, a-z, 0-9, $, # 等. Oracl ...
- lintcode:二叉树的中序遍历
题目: 二叉树的中序遍历 给出一棵二叉树,返回其中序遍历 样例 给出二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [1,3,2]. 挑战 你能使用非递归算法来实现么? 解题: 程序直接来源 ...
- iOS开发--即时通讯
什么是环信? 1.环信是一个第三平台,提供即时通信(IM–Instant Messaging )的服务 2.环信是在XMPP的基础上进行二次开发 3.环信在网络上传输的数据也是XML 4.使用环信,不 ...
- 58. Length of Last Word
题目: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return t ...