c# 多线程2 源码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ConsoleApplication1
{
    class Test
    {
        static SalesLady saleslady = new SalesLady(14, 0, 0);
        static EventWaitHandle wh = new AutoResetEvent(false);
        //static void Main()
        //{
        //    int[] moneies = { 10, 10, 5, 10, 5, 10, 5, 5, 10, 5, 10, 5, 5, 10, 5 };
        //    Thread[] aThreadArray = new Thread[20];
        //    Console.WriteLine("现在开始售票:");
        //    for (int i = 0; i < moneies.Length; i++)
        //    {
        //        CustomerClass cc = new CustomerClass(i + 1, moneies[i]);
        //        aThreadArray[i] = new Thread(cc.run);
        //        aThreadArray[i].Start();
        //        // Thread.Sleep(0);//当前线程让出时间片 如果有上个被唤醒的线程 让其先执行
        //    }
        //    for (int i = 0; i < moneies.Length; i++)
        //    {
        //        try
        //        {
        //            aThreadArray[i].Join();
        //        }
        //        catch (Exception e)
        //        {
        //            Console.WriteLine(e.ToString());
        //        }
        //    }
        //    Console.WriteLine("票已售完" + saleslady.memontoes);
        //    Console.Read();
        //}


        class SalesLady
        {
            public int memontoes, five, ten;
            public SalesLady(int m, int f, int t)
            {
                memontoes = m;
                five = f;
                ten = t;
            }
            public String ruleForSale(int num, int money)//同步的方法(同时对多个线程可见的方法)同时只能有1个线程运行
            {
                 
                String s = null;
                if (memontoes <= 0)
                    return "对不起,已经售完";
                if (money == 5)
                {
                    memontoes--;
                    five++;
                    s = "给你票,你的钱正好。";
                    //notifyAll();   
                    wh.Set(); //唤醒
                }
                else if (money == 10)
                {
                    while (five < 1)
                    {
                        try
                        {
                            Console.WriteLine("" + num + "号顾客用10元购票,请等待");
                            //wait();
                            wh.WaitOne(); //
                            Thread.Sleep(1);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.ToString());
                        }
                        // 如果你的线程能够运行到这里,那么一定有一个five,此时就看哪个线程先被执行了
                        // 因为是同步方法,其中获得运行权利的线程,必须运行结束才会让其它的线程运行
                        // 所以当再次判断时,那个finve又没有了。
                        // 不会出现没有five却找零的问题。
                    }
                    // 如果你的线程能够运行到这里,那么其一定有一个five,
                    // 所以这个线程不会出现没有five而找零的问题
                    if (memontoes <= 0)
                    {
                        return "对不起,已经售完";
                    }
                    memontoes--;
                    five -= 1;
                    ten++;
                    s = "给你票,找你5元。";
                }
                 
                return s;
            }
        }
        // 顾客
        class CustomerClass
        {
            int num, money;
            public void run()
            {
                Console.WriteLine("我是" + num + "号顾客,用" + money + "元购票,售票员说:"
                    + saleslady.ruleForSale(num, money));

            }
            public CustomerClass(int n, int m)
            {
                num = n;
                money = m;
            }
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值