Single Thread Execution设计模式
public class Test { public static void main(String[] args){ // FlightSercurityTest.test(); // EatNoodleThread.test(); EatNoodleThread1.test(); } } /* 16.1.1 非线程安全 */ class FlightSecurity{ private int count = 0; private String boardingPass = "null"; private String idCard = "null"; public synchronized void pass(String boardingPass, String idCard) { this.boardingPass = boardingPass; this.idCard = idCard; this.count++; check(); } private void check(){ if (boardingPass.charAt(0) != idCard.charAt(0)) { throw new RuntimeException("---Exception---"+toString()); } } @Override public String toString() { return "FlightSecurity{" + "count=" + count + ", boardingPass='" + boardingPass + '\'' + ", idCard='" + idCard + '\'' + '}'; } } class FlightSercurityTest{ static class Passenagers extends Thread{ private final FlightSecurity flightSecurity; private final String isCard; private final String boardingPass; public Passenagers(FlightSecurity flightSecurity, String isCard, String boardingPass) { this.flightSecurity = flightSecurity; this.isCard = isCard; this.boardingPass = boardingPass; } public void run(){ while (true){ flightSecurity.pass(boardingPass,isCard); } } } public static void test(){ final FlightSecurity f= new FlightSecurity(); new Passenagers(f,"A","A").start(); new Passenagers(f,"B","B").start(); new Passenagers(f,"C","C").start(); } } /* 16.3 吃面问题 */ class Tableware{ private final String toolName; public Tableware(String toolName) { this.toolName = toolName; } @Override public String toString() { return "Tableware: "+toolName; } } class TablewarePair{ private final Tableware left; private final Tableware right; public TablewarePair(Tableware left, Tableware right) { this.left = left; this.right = right; } public Tableware getLeft(){ return left; } public Tableware getRight(){ return right; } } class EatNoodleThread extends Thread{ private final String name; private final Tableware left; private final Tableware right; public EatNoodleThread(String name, Tableware left, Tableware right) { this.name = name; this.left = left; this.right = right; } public void run(){ while(true){ this.eat(); } } private void eat(){ synchronized (left){ synchronized (right){ System.out.println(name+" take up "+left+"(left)"); synchronized (right){ System.out.println(name+" take up "+right+"(right)"); System.out.println(name+" is eating now."); System.out.println(name+" put down "+right+"(right)"); } System.out.println(name+" put down "+left+"(left)"); } } } public static void test(){ Tableware fork = new Tableware("fork"); Tableware knife = new Tableware("knife"); new EatNoodleThread("Big",fork,knife).start(); new EatNoodleThread("Little",knife,fork).start(); } } class EatNoodleThread1 extends Thread{ private final String name; private final TablewarePair pair; public EatNoodleThread1(String name, TablewarePair pair) { this.name = name; this.pair = pair; } public void run() { while (true) { this.eat(); } } private void eat(){ synchronized (pair){ System.out.println(name+" take up "+pair.getLeft()+"(left)"); System.out.println(name+" put down "+pair.getRight()+"(right)"); System.out.println(name+" is eating now."); System.out.println(name+" take up "+pair.getLeft()+"(right)"); System.out.println(name+" put down "+pair.getLeft()+"(left)"); } } public static void test(){ Tableware fork = new Tableware("fork"); Tableware knife = new Tableware("knife"); TablewarePair pair = new TablewarePair(fork,knife); new EatNoodleThread1("A",pair).start(); new EatNoodleThread1("B",pair).start(); } }
Single Thread Execution设计模式的更多相关文章
- 多线程系列之二:Single Thread Execution 模式
一,什么是SingleThreadExecution模式?同一时间内只能让一个线程执行处理 二,例子 1.不安全的情况 用程序模拟 三个人频繁地通过一个只允许一个人经过的门.当人通过时,统计人数便会增 ...
- Single Thread Execution 能通过这座桥的只有一个人
直奔主题, Single Thread Execution也称作Critical Section(临界区),范例如下: public class SingleThreadGate { public s ...
- JAVA并发设计模式学习笔记(二)—— Single Threaded Execution Pattern
注:本文的主要参考资料为结城浩所著<JAVA多线程设计模式>. 单线程执行模式(Single Threaded Execution Pattern)是最简单的多线程设计模式,几乎所有其他的 ...
- 多线程设计模式(一) Single Threaded Execution
这里有一座独木桥.因为桥身非常的细,一次只能允许一个人通过.当这个人没有下桥,另一个人就不能过桥.如果桥上同时又两个人,桥就会因为无法承重而破碎而掉落河里. 这就是Single Threaded Ex ...
- 多线程程序设计学习(2)之single threaded execution pattern
Single Threaded Execution Pattern[独木桥模式] 一:single threaded execution pattern的参与者--->SharedResourc ...
- 多线程学习之一独木桥模式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 ...
随机推荐
- WebAPI增加Area以支持无限层级同名Controller
原文:WebAPI增加Area以支持无限层级同名Controller 微软的WebAPI默认实现逻辑 默认实现中不支持同名Controller,否则在访问时会报HttpError,在网上找到了各种路由 ...
- 使用.NET进行高效率互联网敏捷开发的思考和探索【一、概述】
不知从什么时候开始,创业变得很廉价,谈什么都是互联网,动辄融资千万.这阵风好像也刮向了程序员中,有那么一大批开发者,数据结构不好好学习.数据库原理不扎实掌握,在github上发布几个项目,用nodej ...
- Delphi用Socket API实现路由追踪
Windows自带的Tracert是向远程主机发送ICMP包进行追踪,但是目前很多主机关闭了ICMP答复,这个工具不太好使了~~~~~原理咱知道,正规的Trace不就是发送TTL依次递增的UDP包吗? ...
- Qt:解析命令行(使用QCommandLineOption和QCommandLineParser)
Qt从5.2版开始提供了两个类QCommandLineOption和QCommandLineParser来解析应用的命令行参数. 一.命令行写法命令行:"-abc" 在QComma ...
- jQuery.form的使用方法
首先需要引入jquery.form.js 之后即可使用 jquery.form.js的中文API网址http://www.vaikan.com/docs/jquery.form.plugin/jque ...
- 通过Graphics对象获取它所属的Control
using System.Runtime.InteropServices; [DllImport("user32.dll")] public static extern Int ...
- Notepad2(C语言+Windows消息写的,24592行代码)
C语言+Windows消息写的,24592行代码 http://www.flos-freeware.ch/
- 减少Qt编译时间暨简单Qt裁剪
本站所有文章由本站和原作者保留一切权力,仅在保留本版权信息.原文链接.原文作者的情况下允许转载,转载请勿删改原文内容, 并不得用于商业用途. 谢谢合作.原文链接:减少Qt编译时间暨简单Qt裁剪 编译一 ...
- 你真的懂printf么?
自从你进入程序员的世界,就开始照着书本编写着各种helloworld,大笔一挥: printf("Hello World!\n"); 于是控制台神奇地出现了一行字符串,计算机一句温 ...
- Spring Boot入门篇(基于Spring Boot 2.0系列)
1:概述: Spring Boot是用来简化Spring应用的初始化开发过程. 2:特性: 创建独立的应用(jar|war形式); 需要用到spring-boot-maven-plugin插件 直接嵌 ...