zoukankan      html  css  js  c++  java
  • AutoResetEvent 和 ManualResetEvent 多线程应用

    AutoResetEvent 

    1.用于在多线程,对线程进行阻塞放行

    static AutoResetEvent auth0 = new AutoResetEvent(false);
    static AutoResetEvent auth1 = new AutoResetEvent(false);
    static void Main(string[] args)
            {
    
                Thread th = new Thread(t0);
                th.IsBackground = true;
                th.Start();
    
                Thread th1 = new Thread(t1);
                th1.IsBackground = true;
                th1.Start();
    
                Console.ReadLine();
            }
    static void t0()
            {
                Console.WriteLine("1");
                auth1.Set();
    
                auth0.WaitOne();
                Console.WriteLine("3");
                auth1.Set();
    
                auth0.WaitOne();
                Console.WriteLine("5");
                auth1.Set();
            }
            static void t1()
            {
                auth1.WaitOne();
                Console.WriteLine("2");
                auth0.Set();
    
                auth1.WaitOne();
                Console.WriteLine("4");
                auth0.Set();
    
                auth1.WaitOne();
                Console.WriteLine("6");
            }

    多个线程对应多个 AutoResetEvent  实例,初始化设置阻塞false,WaitOne进行阻塞,当Set之后阻塞变成true程序进行,另外WaitOne之后AutoResetEvent会自动变成fase。

    Set之后,若多个线程都WaitOne,会随机向某个线程发送继续执行的信号。

    执行结果如图

    ManualResetEvent 

    2.Set之后多个线程会收到放行信号

    static ManualResetEvent mAuth0 = new ManualResetEvent(false);
    static ManualResetEvent mAuth1 = new ManualResetEvent(false);
    static CircleQueue<bool> MyQueue = new CircleQueue<bool>(2);
    static void Main(string[] args)
            {
                Thread th = new Thread(t0);
                th.IsBackground = true;
                th.Start();
    
                Thread th1 = new Thread(t1);
                th1.IsBackground = true;
                th1.Start();
    
                Thread th2 = new Thread(t2);
                th2.IsBackground = true;
                th2.Start();
    
                Console.ReadLine();
            }
            static void t0()
            {
                Console.WriteLine("1-0");
                mAuth1.Set();
    
                mAuth0.WaitOne();
                Console.WriteLine("2-0");
                mAuth1.Set();
            }
            static void t1()
            {
                bool r = mAuth1.WaitOne();
                MyQueue.EnQueue(r);
                Console.WriteLine("2-1");
                while (true)
                {
                    bool[] ListA = MyQueue.GetAllQueue();
                    if (ListA.Contains(false) == false)
                    {
                        break;
                    }
                    Thread.Sleep(50);
                }
                mAuth1.Reset();
                mAuth0.Set();
    
                mAuth1.WaitOne();
                Console.WriteLine("3-1");
            }
            static void t2()
            {
                bool r = mAuth1.WaitOne();
                MyQueue.EnQueue(r);
                Console.WriteLine("2-2");
                while (true)
                {
                    bool[] ListA = MyQueue.GetAllQueue();
                    if (ListA.Contains(false) == false)
                    {
                        break;
                    }
                    Thread.Sleep(50);
                }
                mAuth1.Reset();
                mAuth0.Set();
    
                mAuth1.WaitOne();
                Console.WriteLine("3-2");
            }

    运行结果

    其中用到了环形队列,下次再讨论。

    欢迎指正:haizi2014@qq.com
  • 相关阅读:
    第五周
    第三章 程序的机器级表示
    第二章 信息的表示和处理
    嵌入式Linux应用开发——Linux下的C编程基础
    Linux基础入门(20135207 王国伊)
    Java实验报告(实验四)
    linux系统之pam模块
    linux 从入门到跑路-时间,日期问题
    linux 从入门到跑路-Shell和部分命令
    linux 从入门到跑路-挂载,命令的执行顺序
  • 原文地址:https://www.cnblogs.com/hcfan/p/7894822.html
Copyright © 2011-2022 走看看