zoukankan      html  css  js  c++  java
  • c# 多线程编程中AutoResetEvent和ManualResetEvent

    作为等同于Java的wait,notify,notifyAll的存在,AutoResetEvent和ManualResetEvent分别实现了notify和notifyAll的功能,下面的代码简单讲解了一下实现原理,并展示了这一过程。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    
    namespace ThreadTest
    {
        class Program
        {
            //新建一个默认无信号的手动重置线程事件
            private void Test()
            {
                ManualResetEvent manualResetEvent = new ManualResetEvent(false);
                AutoResetEvent autoResetEvent = new AutoResetEvent(false);
                //线程传参需要通过ParameterizedThreadStart委托,并在Start时附加参数。
                new Thread(new ParameterizedThreadStart(MyThreadHandler)).Start(new ParamStruct("Thread1", manualResetEvent));
                new Thread(new ParameterizedThreadStart(MyThreadHandler)).Start(new ParamStruct("Thread2", manualResetEvent));
                new Thread(new ParameterizedThreadStart(MyThreadHandler)).Start(new ParamStruct("Thread3", autoResetEvent));
                new Thread(new ParameterizedThreadStart(MyThreadHandler)).Start(new ParamStruct("Thread4", autoResetEvent));
                Thread.Sleep(1000);
                //manualResetEvent在Set方法中,会让信号状态从无到有,从而让所有wait状态的线程唤醒,实现NotifyAll
                manualResetEvent.Set();
                //autoResetEvent在Set方法中,会在第一个处于wait状态的线程唤醒后将信号状态Reset,变为无信号,实现NotifyOne
                autoResetEvent.Set();
                Thread.Sleep(1000);
                autoResetEvent.Set();
                Console.ReadKey();
            }
            static void Main(string[] args)
            {
                new Program().Test();
            }
            private void MyThreadHandler(object obj)
            {
                if (obj is ParamStruct paramStruct)
                {
                    Console.WriteLine(paramStruct.threadName);
                    paramStruct.resetEvent.WaitOne();
                    Console.WriteLine(paramStruct.threadName + " finished on " + DateTime.Now);
                }
            }
        }
        class ParamStruct
        {
            public string threadName;
            public EventWaitHandle resetEvent;
            public ParamStruct(string threadName, EventWaitHandle resetEvent)
            {
                this.threadName = threadName;
                this.resetEvent = resetEvent;
            }
        }
    }
    
  • 相关阅读:
    借书证信息管理系统,C语言实现
    以太坊 助记词提取 账户 公钥 私钥 最新实现可用。
    solc@0.6.3 web3@1.2.6 都是最新版本的,编译与部署示例
    ganache gas 错误
    MFC 记事本 文本编辑器
    课程设计 C语言 学生成绩管理系统
    cmake(.os .a)
    git recommend(alive)
    tensorflow tfrecoder read write
    300. 最长上升子序列
  • 原文地址:https://www.cnblogs.com/cielosun/p/7300369.html
Copyright © 2011-2022 走看看