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设计模式的更多相关文章

  1. 多线程系列之二:Single Thread Execution 模式

    一,什么是SingleThreadExecution模式?同一时间内只能让一个线程执行处理 二,例子 1.不安全的情况 用程序模拟 三个人频繁地通过一个只允许一个人经过的门.当人通过时,统计人数便会增 ...

  2. Single Thread Execution 能通过这座桥的只有一个人

    直奔主题, Single Thread Execution也称作Critical Section(临界区),范例如下: public class SingleThreadGate { public s ...

  3. JAVA并发设计模式学习笔记(二)—— Single Threaded Execution Pattern

    注:本文的主要参考资料为结城浩所著<JAVA多线程设计模式>. 单线程执行模式(Single Threaded Execution Pattern)是最简单的多线程设计模式,几乎所有其他的 ...

  4. 多线程设计模式(一) Single Threaded Execution

    这里有一座独木桥.因为桥身非常的细,一次只能允许一个人通过.当这个人没有下桥,另一个人就不能过桥.如果桥上同时又两个人,桥就会因为无法承重而破碎而掉落河里. 这就是Single Threaded Ex ...

  5. 多线程程序设计学习(2)之single threaded execution pattern

    Single Threaded Execution Pattern[独木桥模式] 一:single threaded execution pattern的参与者--->SharedResourc ...

  6. 多线程学习之一独木桥模式Single Threaded Execution Pattern

    Single Threaded Execution Pattern[独木桥模式] 一:single threaded execution pattern的参与者--->SharedResourc ...

  7. How does a single thread handle asynchronous code in JavaScript?

    原文:https://www.quora.com/How-does-a-single-thread-handle-asynchronous-code-in-JavaScript ----------- ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. ARTS 1.7 - 1.11

    每周一个 Algorithm,Review 一篇英文文章,总结一个工作中的技术 Tip,以及 Share 一个传递价值观的东西! Algorithm: 学习算法 题目: https://leetcod ...

  2. 使用xargs同步文本中单词出现个数

    #!/bin/bash # 分析一个文本文件中单词出现的频率. # 使用 'xargs' 将文本行分解为单词. # 检查命令行上输入的文件. ARGS= E_BADARGS= E_NOFILE= if ...

  3. ML:吴恩达 机器学习 课程笔记(Week7~8)

    Support Vector Machines Unsupervised Learning Dimensionality Reduction

  4. CMake编译Widget UI Qt程序

    自从CMake被引入到KDE项目的编译系统中后,CMake的使用者日益增多,Qt也不例外,除了使用QMAKE编译Qt程序外,也可以使用CMake来编译Qt程序,并且CMake在使用上更灵活,特别是大型 ...

  5. python-监控服务

    最近写了一个web测试程序,因为部署在其他地方,所以想弄个监控的进程去看服务是不是还在,要是不在好发邮件,就用python简单的写了一个. 想法是这样的,单独运行一个monitor的脚本,每隔一段时间 ...

  6. Mariadb的安装与使用

    一.安装Mariadb 参考博客:https://www.cnblogs.com/pyyu/p/9467289.html 安装软件的三中方式 yum原码编译安装下载rpm安装 yum与原码编译安装安装 ...

  7. java.lang.ClassNotFoundException: org.jaxen.JaxenException 解决方法

    当遇到如下exception时, May 11, 2017 4:23:17 PM org.apache.catalina.core.StandardWrapperValve invoke SEVERE ...

  8. ZooKeeper学习第五期--ZooKeeper管理分布式环境中的数据(转)

    转载来源:https://www.cnblogs.com/sunddenly/p/4092654.html 引言 本节本来是要介绍ZooKeeper的实现原理,但是ZooKeeper的原理比较复杂,它 ...

  9. AppBoxFuture: 大数据表分区的3种策略

      之前的文章"分而治之"在介绍大表分区时,作者尚未实现不同的分区策略,即只能按指定的分区键进行分区.这次作者完善了一下分区策略,在规划大表分区时可以按Hash或者时间范围进行分区 ...

  10. 查看oracle/mysql数据库版本号

    1.1.       ORACLE 软件版本 使用oracle用户登录,输入echo "select * from v\$version;"|sqlplus -S / as sys ...