zoukankan      html  css  js  c++  java
  • ManualResetEvent详解

     ManualResetEvent 允许线程通过发信号互相通信。通常,此通信涉及一个线程在其他线程进行之前必须完成的任务。当一个线程开始一个活动(此活动必须完成后,其他线程才能开始)时,它调用 Reset 以将 ManualResetEvent 置于非终止状态,此线程可被视为控制 ManualResetEvent。调用 ManualResetEvent 上的 WaitOne 的线程将阻止,并等待信号。当控制线程完成活动时,它调用 Set 以发出等待线程可以继续进行的信号。并释放所有等待线程。一旦它被终止,ManualResetEvent 将保持终止状态(即对 WaitOne 的调用的线程将立即返回,并不阻塞),直到它被手动重置。可以通过将布尔值传递给构造函数来控制 ManualResetEvent 的初始状态,如果初始状态处于终止状态,为 true;否则为 false。

    //ManualResetEvent的整体思路是在相关线程外部通过
    //手动设置ManualResetEvent.Set()和ReSet()来决定相关线程是否运行
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    namespace ManualResetEventDemo
    {
        class MREDemo
        {
            private ManualResetEvent _mre;
    
            public MREDemo()
            {
                this._mre = new ManualResetEvent(true);
            }
    
            public void CreateThreads()//创建两个线程都是调用同一个函数
            {
                Thread t1 = new Thread(new ThreadStart(Run));
                t1.Start();
    
                Thread t2 = new Thread(new ThreadStart(Run));
                t2.Start();
            }
            public void Set()
            {
                this._mre.Set();//将事件状态设置为终止状态,允许一个或多个等待线程继续
            }
            public void Reset()
            {
                this._mre.Reset();//将事件设置为非终止状态导致线程阻止
            }
            private void Run()
            {
                string strThreadID = string.Empty;
                try
                {
                    while (true)
                    {
                        // 阻塞当前线程
                        this._mre.WaitOne();
    
                        strThreadID = Thread.CurrentThread.ManagedThreadId.ToString();
                        Console.WriteLine("Thread(" + strThreadID + ") is running...");
    
                        Thread.Sleep(5000);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("线程(" + strThreadID + ")发生异常!错误描述:" + ex.Message.ToString());
                }
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("****************************");
                Console.WriteLine("输入"stop"停止线程运行...");
                Console.WriteLine("输入"run"开启线程运行...");
                Console.WriteLine("****************************
    ");
                MREDemo objMRE = new MREDemo();
                objMRE.CreateThreads();
                while (true)
                {
                    string input = Console.ReadLine();
                    if (input.Trim().ToLower() == "stop")
                    {
                        Console.WriteLine("线程已停止运行...");
                        objMRE.Reset();
                    }
                    else if (input.Trim().ToLower() == "run")
                    {
                        Console.WriteLine("线程开启运行...");
                        objMRE.Set();
                    }
                }
            }
        }
    }




  • 相关阅读:
    牛客练习赛51 D题
    Educational Codeforces Round 72 (Rated for Div. 2) C题
    Codeforces Round #583 (Div. 1 + Div. 2, based on Olympiad of Metropolises) C题
    Codeforces Round #583 (Div. 1 + Div. 2, based on Olympiad of Metropolises) A题
    Codeforces Round #583 (Div. 1 + Div. 2, based on Olympiad of Metropolises) A题
    Educational Codeforces Round 72 (Rated for Div. 2) B题
    Educational Codeforces Round 72 (Rated for Div. 2) A题
    《DSP using MATLAB》Problem 7.2
    《DSP using MATLAB》Problem 7.1
    《DSP using MATLAB》Problem 6.24
  • 原文地址:https://www.cnblogs.com/zztong/p/6695151.html
Copyright © 2011-2022 走看看