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 ...
随机推荐
- 【Linux】简单明了查看内存使用和ubuntu的版本号及位数
1.查看ubuntu的版本号:cat /etc/issue 2.查看系统是32位的还是64位:getconf LONG_BIT 3.查看内存使用 free free命令可以用来查看系统内存使用情况,- ...
- 微信小程序把玩(九)scroll-view组件
原文:微信小程序把玩(九)scroll-view组件 scroll-view为滚动视图,分为水平滚动和垂直滚动.注意滚动视图垂直滚动时一定要设置高度否则的话scroll-view不会生效.滚动视图常用 ...
- scp 专题
Tips:阿里云中需要使用内网ip,否则会一直阻塞Linux scp命令用于Linux之间复制文件和目录,具体如何使用这里好好介绍一下,从本地复制到远程.从远程复制到本地是两种使用方式.这里有具体举例 ...
- C#WebBroswer控件的使用
在WebBroswer中可以嵌入一个网页文件,通过Url属性绑定. URI,统一资源标识符,用来唯一的标识一个资源. URL,统一资源定位器,它是一种具体的URI,即URL可以用来标识一个资源. 它包 ...
- T4随记
关于T4模板的信息我就不赘述了,百度一大堆 MSDN的介绍 https://msdn.microsoft.com/zh-cn/library/bb126478.aspx 下面是简单的一个示例,从类中获 ...
- SQLite实现内存键值存储
SQLite数据文件往Linux内存文件系统/dev/shm/data.sqlite3一放,就是内存级读写性能的SQL系统.用SQLite实现内存键值存储:CREATE TABLE IF NOT EX ...
- linux-deployment
官方 linux-deploymenthttp://doc.qt.io/qt-5/linux-deployment.html linuxdeployqthttps://github.com/probo ...
- Google Breakpad 在 windows下捕获程序崩溃报告
http://blog.csdn.net/goforwardtostep/article/details/56304285
- JavaScript-DOM续
一.路由的跳转 <body> <a href="#/course">课程</a> <a href="#/main"&g ...
- SYN4505型 标准同步时钟
SYN4505型 标准同步时钟 标准同步时钟电厂时间同步使用说明视频链接: http://www.syn029.com/h-pd-245-0_310_1_-1.html 请将此链接复制到浏览器打开观看 ...