模拟多人不同面值购票找零的多线程代码

一个网友的代码,看着还不错,就修正了BUG,并重新整理了一下部分算法。
  1. /**
  2.  * 模拟多人不同面值购票找零的多线程代码。
  3.  * 
  4.  * @author leoklkj,赵学庆 java2000.net
  5.  */
  6. public class T {
  7.   static SalesLady saleslady = new SalesLady(1400);
  8.   public static void main(String[] args) {
  9.     new T().start();
  10.   }
  11.   public void start() {
  12.     int moneies[] = { 1010510510551051055105 };
  13.     Thread[] aThreadArray = new Thread[20];
  14.     System.out.println("现在开始售票:");
  15.     for (int i = 0; i < moneies.length; i++) {
  16.       aThreadArray[i] = new Thread(new CustomerClass(i + 1, moneies[i]));
  17.       aThreadArray[i].start();
  18.     }
  19.     for (int i = 0; i < moneies.length; i++) {
  20.       try {
  21.         aThreadArray[i].join();
  22.       } catch (InterruptedException e) {
  23.         e.printStackTrace();
  24.       }
  25.     }
  26.     System.out.println("票已售完" + saleslady.memontoes);
  27.   }
  28. }
  29. // 顾客 销售人员
  30. class SalesLady {
  31.   int memontoes, five, ten;
  32.   public synchronized String ruleForSale(int num, int money) {
  33.     String s = null;
  34.     if (memontoes <= 0)
  35.       return "对不起,已经售完";
  36.     if (money == 5) {
  37.       memontoes--;
  38.       five++;
  39.       s = "给你票,你的钱正好。";
  40.       notifyAll();
  41.     } else if (money == 10) {
  42.       while (five < 1) {
  43.         try {
  44.           System.out.println("" + num + "号顾客用10元购票,请等待");
  45.           wait();
  46.         } catch (InterruptedException e) {
  47.         }
  48.         // 如果你的线程能够运行到这里,那么一定有一个five,此时就看哪个线程先被执行了
  49.         // 因为是同步方法,其中获得运行权利的线程,必须运行结束才会让其它的线程运行
  50.         // 所以当再次判断时,那个finve又没有了。
  51.         // 不会出现没有five却找零的问题。
  52.       }
  53.       // 如果你的线程能够运行到这里,那么其一定有一个five,
  54.       // 所以这个线程不会出现没有five而找零的问题
  55.       if (memontoes <= 0) {
  56.         return "对不起,已经售完";
  57.       }
  58.       memontoes--;
  59.       five -= 1;
  60.       ten++;
  61.       s = "给你票,找你5元。";
  62.     }
  63.     return s;
  64.   }
  65.   SalesLady(int m, int f, int t) {
  66.     memontoes = m;
  67.     five = f;
  68.     ten = t;
  69.   }
  70. }
  71. // 顾客
  72. class CustomerClass implements Runnable {
  73.   int num, money;
  74.   public void run() {
  75.     System.out.println("我是" + num + "号顾客,用" + money + "元购票,售票员说:"
  76.         + T.saleslady.ruleForSale(num, money));
  77.   }
  78.   CustomerClass(int n, int m) {
  79.     num = n;
  80.     money = m;
  81.   }
  82. }

某一个运行结果
现在开始售票:
1号顾客用10元购票,请等待
2号顾客用10元购票,请等待
我是3号顾客,用5元购票,售票员说:给你票,你的钱正好。
我是4号顾客,用10元购票,售票员说:给你票,找你5元。
2号顾客用10元购票,请等待
1号顾客用10元购票,请等待
我是5号顾客,用5元购票,售票员说:给你票,你的钱正好。
我是1号顾客,用10元购票,售票员说:给你票,找你5元。
2号顾客用10元购票,请等待
我是2号顾客,用10元购票,售票员说:给你票,找你5元。
我是10号顾客,用5元购票,售票员说:给你票,你的钱正好。
我是13号顾客,用5元购票,售票员说:给你票,你的钱正好。
我是8号顾客,用5元购票,售票员说:给你票,你的钱正好。
我是9号顾客,用10元购票,售票员说:给你票,找你5元。
我是7号顾客,用5元购票,售票员说:给你票,你的钱正好。
我是6号顾客,用10元购票,售票员说:给你票,找你5元。
我是12号顾客,用5元购票,售票员说:给你票,你的钱正好。
我是11号顾客,用10元购票,售票员说:给你票,找你5元。
我是14号顾客,用10元购票,售票员说:给你票,找你5元。
我是15号顾客,用5元购票,售票员说:对不起,已经售完
票已售完0


坛地址: http://topic.csdn.net/u/20081016/00/95ab6a1c-bda4-4be8-b23e-fa7df1d61399.html
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值