C# 多线程 AutoResetEvent和ManualResetEvent

本文演示了一个模拟图书购买流程的多线程程序。通过创建三个独立线程分别执行扫描书籍、付款和取书的任务,并利用AutoResetEvent和ManualResetEvent进行线程间的同步与通信。展示了如何使用C#中的Thread类和事件对象来协调多线程操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


namespace MutiThreadTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Thread SeekBookThread = new Thread(new ThreadStart(TestMutiThrd.SeekProc));
            SeekBookThread.Name = "扫描线程";
            SeekBookThread.Start();


            Thread payMoneyThread = new Thread(new ThreadStart(TestMutiThrd.PayMoneyProc));
            payMoneyThread.Name = "付钱线程";
            payMoneyThread.Start();


            Thread getBookThread = new Thread(new ThreadStart(TestMutiThrd.GetBookProc));
            getBookThread.Name = "取书线程";
            getBookThread.Start();


            for (int i = 1; i <= TestMutiThrd.numIterations; i++)
            {
                Console.WriteLine("买书线程:书本{0}", i);
                TestMutiThrd.number = i;
                //Signal that a value has been written.
                TestMutiThrd.SekEvent.Set();//解锁扫描线程
                TestMutiThrd.buyResetEvent.WaitOne();//等待买书所有线程执行结束,才能继续
                Thread.Sleep(0);
            }
            Thread.Sleep(1000);
            Console.ReadLine();
            SeekBookThread.Abort();
            payMoneyThread.Abort();
            getBookThread.Abort();
        }
    }


    public class TestMutiThrd
    {
        public const int numIterations = 10;
        //买书
        public static AutoResetEvent buyResetEvent = new AutoResetEvent(false);
        //扫描
        public static AutoResetEvent SekEvent = new AutoResetEvent(false);
        //付钱
        public static AutoResetEvent PayEvent = new AutoResetEvent(false);
        //取书
        public static AutoResetEvent GetEvent = new AutoResetEvent(false);
        //付钱和取书
        public static ManualResetEvent PayGetEvent = new ManualResetEvent(false);


        public static ReaderWriterLock RWLock = new ReaderWriterLock();
        //static ManualResetEvent myResetEvent = new ManualResetEvent(false);
        //static ManualResetEvent ChangeEvent = new ManualResetEvent(false);
        public static int number; //这是关键资源
        public static TestMutiThrd OTestMutiThrd = new TestMutiThrd();


        public int NumIterations
        {
            get
            {
                return numIterations;
            }
        }


        /// <summary>
        /// 扫描过程
        /// </summary>
        public static void SeekProc()
        {
            while (true)
            {
                SekEvent.WaitOne();
                //SekEvent.Reset();
                Console.WriteLine("{0}:书本{1}", Thread.CurrentThread.Name, number);
                Thread.Sleep(0);
                //PayEvent.Set();//解锁付款线程
                PayGetEvent.Set();//同时解锁付款和取书线程
            }
        }


        /// <summary>
        /// 付钱过程
        /// </summary>
        public static void PayMoneyProc()
        {
            while (true)
            {
                PayGetEvent.WaitOne();
                //PayEvent.WaitOne();
                Console.WriteLine("{0}:书本{1}", Thread.CurrentThread.Name, number);
                Thread.Sleep(10);
                //GetEvent.Set();//解锁取书线程
            }
        }


        /// <summary>
        /// 取书过程
        /// </summary>
        public static void GetBookProc()
        {
            while (true)
            {
                PayGetEvent.WaitOne();
                PayGetEvent.Reset();//不加 无限执行下去
                //GetEvent.WaitOne();         
                Console.WriteLine("{0}:书本{1}", Thread.CurrentThread.Name, number);
                Console.WriteLine("------------------------------------------");
                Thread.Sleep(10);
                buyResetEvent.Set();//解锁买书线程
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值