java 多线程 购票

 
  1. package test;
  2. class SalesLady {
  3.     int memontoes, five, ten;
  4.     public synchronized String ruleForSale(int num, int money) {
  5.         String s = null;
  6.         if (memontoes <= 0)
  7.             return "sorry,ticket have been sold out";//"对不起,已经售完";
  8.         if (money == 5) {
  9.             if (memontoes > 0) {
  10.                 memontoes--;
  11.                 five++;
  12.                 s = "give your ticket," + "the money is just enought.";
  13.                 //System.out.println("tickets:"+memontoes+" five:"+five+" ten:"+ten);
  14.             }else return "sorry,ticket have been sold out";
  15.         } else if (money == 10) {
  16.             while (five < 1) {
  17.                 try {
  18.                     System.out.println("No " + num + " Customer use 10 yuan to purchase ticket,please wait a moment");
  19.                     wait();
  20.                 } catch (InterruptedException e) {
  21.                 }
  22.             }
  23.             if (memontoes > 0) {
  24.                 memontoes--;
  25.                 five -= 1;
  26.                 ten++;
  27.                 s = "give your ticket," + "change 5 yuan.";
  28.                 //System.out.println("tickets:"+memontoes+" five:"+five+" ten:"+ten);
  29.             }else return "sorry,ticket have been sold out";
  30.         }
  31.         notifyAll();
  32.         return s;
  33.     }
  34.     SalesLady(int m, int f, int t) {
  35.         memontoes = m;
  36.         five = f;
  37.         ten = t;
  38.     }
  39. }
  40. public class SaleTick  {
  41.     static SalesLady saleslady = new SalesLady(1400);
  42.     //public void start() {
  43.     public static void main(String[] args) {
  44.         int moneies[] = { 510510510551051055105};
  45.         Thread[] aThreadArray = new Thread[20];
  46.         System.out.println("begin to sale tickets:");
  47.         for (int i = 0; i < moneies.length; i++) {
  48.             aThreadArray[i] = new Thread(new CustomerClass(i + 1, moneies[i]));
  49.             aThreadArray[i].start();
  50.         }
  51.         WhileLoop: while (true) {
  52.             for (int i = 0; i < moneies.length; i++)
  53.                 if (aThreadArray[i].isAlive())
  54.                     continue WhileLoop;
  55.             break;
  56.         }
  57.         System.out.println("ticket have been sold out");
  58.     }
  59. }
  60. class CustomerClass implements Runnable {
  61.     int num, money;
  62.     public void run() {
  63.         try {
  64.             Thread.sleep(1);
  65.         } 
  66.         catch (InterruptedException e) {
  67.         }
  68.         System.out.println("I'm # " + num + " Customer,use " + money + " yuan to purchase ticket,the salesman said:"
  69.                 + SaleTick.saleslady.ruleForSale(num, money));
  70.     }
  71.     CustomerClass(int n, int m) {
  72.         num = n;
  73.         money = m;
  74.     }
  75. }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值