一个网友的代码,看着还不错,就修正了BUG,并重新整理了一下部分算法。
某一个运行结果
现在开始售票:
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
- /**
- * 模拟多人不同面值购票找零的多线程代码。
- *
- * @author leoklkj,赵学庆 java2000.net
- */
- public class T {
- static SalesLady saleslady = new SalesLady(14, 0, 0);
- public static void main(String[] args) {
- new T().start();
- }
- public void start() {
- int moneies[] = { 10, 10, 5, 10, 5, 10, 5, 5, 10, 5, 10, 5, 5, 10, 5 };
- Thread[] aThreadArray = new Thread[20];
- System.out.println("现在开始售票:");
- for (int i = 0; i < moneies.length; i++) {
- aThreadArray[i] = new Thread(new CustomerClass(i + 1, moneies[i]));
- aThreadArray[i].start();
- }
- for (int i = 0; i < moneies.length; i++) {
- try {
- aThreadArray[i].join();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- System.out.println("票已售完" + saleslady.memontoes);
- }
- }
- // 顾客 销售人员
- class SalesLady {
- int memontoes, five, ten;
- public synchronized String ruleForSale(int num, int money) {
- String s = null;
- if (memontoes <= 0)
- return "对不起,已经售完";
- if (money == 5) {
- memontoes--;
- five++;
- s = "给你票,你的钱正好。";
- notifyAll();
- } else if (money == 10) {
- while (five < 1) {
- try {
- System.out.println("" + num + "号顾客用10元购票,请等待");
- wait();
- } catch (InterruptedException e) {
- }
- // 如果你的线程能够运行到这里,那么一定有一个five,此时就看哪个线程先被执行了
- // 因为是同步方法,其中获得运行权利的线程,必须运行结束才会让其它的线程运行
- // 所以当再次判断时,那个finve又没有了。
- // 不会出现没有five却找零的问题。
- }
- // 如果你的线程能够运行到这里,那么其一定有一个five,
- // 所以这个线程不会出现没有five而找零的问题
- if (memontoes <= 0) {
- return "对不起,已经售完";
- }
- memontoes--;
- five -= 1;
- ten++;
- s = "给你票,找你5元。";
- }
- return s;
- }
- SalesLady(int m, int f, int t) {
- memontoes = m;
- five = f;
- ten = t;
- }
- }
- // 顾客
- class CustomerClass implements Runnable {
- int num, money;
- public void run() {
- System.out.println("我是" + num + "号顾客,用" + money + "元购票,售票员说:"
- + T.saleslady.ruleForSale(num, money));
- }
- CustomerClass(int n, int m) {
- num = n;
- money = m;
- }
- }
某一个运行结果
现在开始售票:
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