返回顶部
首页 > 资讯 > 后端开发 > Python >Java模拟实现机场过安检处理流程
  • 954
分享到

Java模拟实现机场过安检处理流程

2024-04-02 19:04:59 954人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

目录一 场景描述二 实战三 测试四 问题分析五 说明一 场景描述 乘飞机时,在进入登机口之前必须经过安全检查,安检口类似于

一 场景描述

乘飞机时,在进入登机口之前必须经过安全检查,安检口类似于独木桥,每次只能通过一个人,工作人员除了检查你的登机牌外,还有一些其它检查。在此模拟一个非线程安全的安检口类,旅客(线程)分别手持登机牌和身份证接受工作人员的检查。

二 实战

1 FlightSecurity

package concurrent.FlightSecurity;
public class FlightSecurity {
    private int count;
    // 登机牌
    private String boardingPass = "null";
    // 身份证
    private String idCard = "null";
    public 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("======Excepton======"+toString());
        }
    }
    public String toString(){
        return "The " +count +" paaaenges,boardingPass [" + boardingPass + "],idCard [" + idCard +"]";
    }
}

2 FightSecurityTest

package concurrent.FlightSecurity;
public class FightSecurityTest {
    // 旅客线程
    static class Passengers extends Thread{
        // 机场安检类
        private final FlightSecurity fightSecurity;
        // 旅客的身份证
        private final String idCard;
        // 旅客登机牌
        private final String boardingPass;
        Passengers(FlightSecurity fightSecurity, String idCard, String boardingPass) {
            this.fightSecurity = fightSecurity;
            this.idCard = idCard;
            this.boardingPass = boardingPass;
        }
        @Override
        public void run() {
            while(true){
                // 旅客不断地过安检
                fightSecurity.pass(boardingPass,idCard);
            }
        }
    }
    public static void main(String[] args) {
        // 定义三个旅客,身份证和登机牌首字母相同
        final FlightSecurity flightSecurity= new FlightSecurity();
        new Passengers(flightSecurity,"A","A").start();
        new Passengers(flightSecurity,"B","B").start();
        new Passengers(flightSecurity,"C","C").start();
    }
}

三 测试

1 测试结果1

Exception in thread "Thread-1" Exception in thread "Thread-0" java.lang.RuntimeException: ======Excepton======The 356 paaaenges,boardingPass [B],idCard [A]
    at concurrent.FlightSecurity.FlightSecurity.check(FlightSecurity.java:20)
    at concurrent.FlightSecurity.FlightSecurity.pass(FlightSecurity.java:14)
    at concurrent.FlightSecurity.FightSecurityTest$Passengers.run(FightSecurityTest.java:24)
java.lang.RuntimeException: ======Excepton======The 356 paaaenges,boardingPass [B],idCard [A]
    at concurrent.FlightSecurity.FlightSecurity.check(FlightSecurity.java:20)
    at concurrent.FlightSecurity.FlightSecurity.pass(FlightSecurity.java:14)
    at concurrent.FlightSecurity.FightSecurityTest$Passengers.run(FightSecurityTest.java:24)

2 测试结果2

Exception in thread "Thread-0" Exception in thread "Thread-1" java.lang.RuntimeException: ======Excepton======The 953 paaaenges,boardingPass [C],idCard [C]
    at concurrent.FlightSecurity.FlightSecurity.check(FlightSecurity.java:20)
    at concurrent.FlightSecurity.FlightSecurity.pass(FlightSecurity.java:14)
    at concurrent.FlightSecurity.FightSecurityTest$Passengers.run(FightSecurityTest.java:24)
java.lang.RuntimeException: ======Excepton======The 1039 paaaenges,boardingPass [C],idCard [C]
    at concurrent.FlightSecurity.FlightSecurity.check(FlightSecurity.java:20)
    at concurrent.FlightSecurity.FlightSecurity.pass(FlightSecurity.java:14)
    at concurrent.FlightSecurity.FightSecurityTest$Passengers.run(FightSecurityTest.java:24)

四 问题分析

多线程情况下调用 pass 方法,虽然参数的传递百分百保证就是这两个值,但是在 pass 方法中对这两个值的赋值很有可能交叉,不能保证原子性操作。

解决的方法是给 pass 方法加上 synchronized 关键字。

public synchronized void pass(String boardingPass,String idCard)

五 说明

在 Java 中经常会听到线程安全的类和线程非安全的类,所谓线程安全的类是指多个线程同时进行操作时,不会引起数据不一致问题,反之则是线程非安全的类,在线程安全的类中经常会看到 synchronized 关键字的身影。

到此这篇关于Java模拟实现机场过安检处理流程的文章就介绍到这了,更多相关Java 机场安检内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Java模拟实现机场过安检处理流程

本文链接: https://lsjlt.com/news/146861.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作