java模拟实现机场过安检处理流程-mile米乐体育
目录
- 一场景描述
- 二实战
- 三测试
- 四问题分析
- 五说明
一场景描述
乘飞机时,在进入登机口之前必须经过安全检查,安检口类似于独木桥,每次只能通过一个人,工作人员除了检查你的登机牌外,还有一些其它检查。在此模拟一个非线程安全的安检口类,旅客(线程)分别手持登机牌和身份证接受工作人员的检查。
二实战
1flightsecurity
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 "]"; } }
2fightsecuritytest
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 = boardihttp://www.cppcns.comngpass; } @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 机场安检内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!