zoukankan      html  css  js  c++  java
  • c#多线程控制

      class Program
        {
            static  Thread SeekBookThread = new Thread(new ThreadStart(TestMutiThrd.SeekProc));
            static  Thread payMoneyThread = new Thread(new ThreadStart(TestMutiThrd.PayMoneyProc));
            static Thread getBookThread = new Thread(new ThreadStart(TestMutiThrd.GetBookProc));
            static void Main(string[] args)
            { 
            
                SeekBookThread.Name = "扫描线程";
                SeekBookThread.Start();
    
    
          
                payMoneyThread.Name = "付钱线程";
                payMoneyThread.Start();
    
    
            
                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 = 50;
            //买书
            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) //(thread.ThreadState != ThreadState.Aborted)
                {
                    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)
                {
                    PayEvent.WaitOne();
                    Console.WriteLine("{0}:书本{1}", Thread.CurrentThread.Name, number);
                    Thread.Sleep(10);
                    GetEvent.Set();//解锁取书线程
                }
            }
    
    
            /// <summary>
            /// 取书过程
            /// </summary>
            public static void GetBookProc()
            {
                while (true)
                {
                    GetEvent.WaitOne();         
                    Console.WriteLine("{0}:书本{1}", Thread.CurrentThread.Name, number);
                    Console.WriteLine("------------------------------------------");
                    Thread.Sleep(10);
                    buyResetEvent.Set();//解锁买书线程
                }
            }
        }
  • 相关阅读:
    jquery的promise和es6的promise的区别
    vue源码
    Paging through lots of data efficiently (and in an Ajax way) with ASP.NET 2.0
    laydate 1.1 下拉日期挡住的bug修复
    Microsoft SQL Server 2008 R2 官方简体中文正式版下载(附激活序列号密钥)
    使用ASP.NET Treeview显示数据库里DataTable里的数据。
    [C# .NET] 数组排序 Arrays
    字符串数据按照大小排序
    Chrome浏览器中的三种刷新模式
    『TensorFlow』lookup.index_table_from_tensor查找表映射函数
  • 原文地址:https://www.cnblogs.com/weifeng123/p/10036513.html
Copyright © 2011-2022 走看看