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

来源:互联网 发布:淘宝图片被盗用怎么办 编辑:程序博客网 时间:2024/05/10 04:27

这篇文章,纯属语言转换文章,引用了他人的思路,修改成C#的线程思想,还有许多不完善的地方,希望在讨论中不断完善.

原来的文章是一个Java写的,作者是java2000_net(老紫竹),一个偶然的机会,我看到了那篇文章,感觉思路挺有趣,就想把他转换成C#的来学习.时间紧凑,到12月初才有空来做.

原文地址:http://blog.csdn.net/java2000_net/archive/2008/10/19/3083836.aspx

  1.     public class Test
  2.     {
  3.         static SalesLady saleslady = new SalesLady(14, 0, 0);
  4.         static EventWaitHandle wh = new AutoResetEvent(false);
  5.         static void Main()
  6.         {
  7.             int[] moneies = { 10, 10, 5, 10, 5, 10, 5, 5, 10, 5, 10, 5, 5, 10, 5 };
  8.             Thread[] aThreadArray = new Thread[20];
  9.             Console.WriteLine("现在开始售票:");
  10.             for (int i = 0; i < moneies.Length; i++)
  11.             {
  12.                 CustomerClass cc = new CustomerClass(i + 1, moneies[i]);
  13.                 aThreadArray[i] = new Thread(cc.run);
  14.                 aThreadArray[i].Start();
  15.                 Thread.Sleep(0);
  16.             }
  17.             for (int i = 0; i < moneies.Length; i++)
  18.             {
  19.                 try
  20.                 {
  21.                     aThreadArray[i].Join();
  22.                 }
  23.                 catch (Exception e)
  24.                 {
  25.                     Console.WriteLine(e.ToString());
  26.                 }
  27.             }
  28.             Console.WriteLine("票已售完" + saleslady.memontoes);
  29.             Console.Read();
  30.         }
  31.         class SalesLady
  32.         {
  33.             public int memontoes, five, ten;
  34.             public SalesLady(int m, int f, int t)
  35.             {
  36.                 memontoes = m;
  37.                 five = f;
  38.                 ten = t;
  39.             }
  40.             public String ruleForSale(int num, int money)
  41.             {
  42.                 String s = null;
  43.                 if (memontoes <= 0)
  44.                     return "对不起,已经售完";
  45.                 if (money == 5)
  46.                 {
  47.                     memontoes--;
  48.                     five++;
  49.                     s = "给你票,你的钱正好。";
  50.                     //notifyAll();   
  51.                     wh.Set(); //唤醒
  52.                 }
  53.                 else if (money == 10)
  54.                 {
  55.                     while (five < 1)
  56.                     {
  57.                         try
  58.                         {
  59.                             Console.WriteLine("" + num + "号顾客用10元购票,请等待");
  60.                             //wait();
  61.                             wh.WaitOne(); //
  62.                             Thread.Sleep(1);
  63.                         }
  64.                         catch (Exception e)
  65.                         {
  66.                             Console.WriteLine(e.ToString());
  67.                         }
  68.                         // 如果你的线程能够运行到这里,那么一定有一个five,此时就看哪个线程先被执行了
  69.                         // 因为是同步方法,其中获得运行权利的线程,必须运行结束才会让其它的线程运行
  70.                         // 所以当再次判断时,那个finve又没有了。
  71.                         // 不会出现没有five却找零的问题。
  72.                     }
  73.                     // 如果你的线程能够运行到这里,那么其一定有一个five,
  74.                     // 所以这个线程不会出现没有five而找零的问题
  75.                     if (memontoes <= 0)
  76.                     {
  77.                         return "对不起,已经售完";
  78.                     }
  79.                     memontoes--;
  80.                     five -= 1;
  81.                     ten++;
  82.                     s = "给你票,找你5元。";
  83.                 }
  84.                 return s;
  85.             }
  86.         }
  87.         // 顾客
  88.         class CustomerClass
  89.         {
  90.             int num, money;
  91.             public void run()
  92.             {
  93.                 Console.WriteLine("我是" + num + "号顾客,用" + money + "元购票,售票员说:"
  94.                     + saleslady.ruleForSale(num, money));
  95.                 
  96.             }
  97.             public CustomerClass(int n, int m)
  98.             {
  99.                 num = n;
  100.                 money = m;
  101.             }
  102.         }
  103.     }

小结

C#的Console.WriteLine的输出也是有先后顺序的,按Java的那种写法,如果不在WaitOne()后面加一个Sleep(1),会让后面的找零先输出的,虽然逻辑上运行没有问题.在这里修改Sleep里的时间也会影响到后面的卖票顺序的.(谁叫你发呆!^-^)

原创粉丝点击