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");
        }

运行结果

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

 

转载于:https://www.cnblogs.com/hcfan/p/7894822.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值