前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >线程 ManualResetEvent 类「建议收藏」

线程 ManualResetEvent 类「建议收藏」

作者头像
全栈程序员站长
发布2022-09-14 10:18:09
4400
发布2022-09-14 10:18:09
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。 Reset(): 当一个线程开始一个活动(此活动必须完成后,其他线程才能开始)时, 它调用 Reset 以将 ManualResetEvent 置于非终止状态。此线程可被视为控制 ManualResetEvent。

为了把状态修改为无信号的,必须调用ReSet()方法。

WaitOne(): 调用ManualResetEvent 上的 WaitOne 的线程将阻止,并等待信号。

Set ()当控制线程完成活动时,它调用 Set 以发出等待线程可以继续进行的信号。并释放所有等待线程。Set将事件状态设置为终止状态,允许一个或多个等待线程继续。

为了把状态修改为有信号的,必须调用Set()方法。

ManualResetEvent对象只能拥有两种状态之一:有信号(True)或无信号(false)。ManualResetEvent类继承于WaitHandle类,其构造函数的参数可确定对象的初始状态。

Set()和Reset()方法返回一个布尔值,表示是否进行了成功的修改。

线程 ManualResetEvent 类「建议收藏」
线程 ManualResetEvent 类「建议收藏」

using System;

线程 ManualResetEvent 类「建议收藏」
线程 ManualResetEvent 类「建议收藏」

using System.Collections.Generic;

线程 ManualResetEvent 类「建议收藏」
线程 ManualResetEvent 类「建议收藏」

using System.Text;

线程 ManualResetEvent 类「建议收藏」
线程 ManualResetEvent 类「建议收藏」

using System.Threading;

线程 ManualResetEvent 类「建议收藏」
线程 ManualResetEvent 类「建议收藏」

namespace ManualResetshiyan

线程 ManualResetEvent 类「建议收藏」
线程 ManualResetEvent 类「建议收藏」

{

线程 ManualResetEvent 类「建议收藏」
线程 ManualResetEvent 类「建议收藏」

class Program

线程 ManualResetEvent 类「建议收藏」
线程 ManualResetEvent 类「建议收藏」

{

线程 ManualResetEvent 类「建议收藏」
线程 ManualResetEvent 类「建议收藏」

static void Main(string[] args)

线程 ManualResetEvent 类「建议收藏」
线程 ManualResetEvent 类「建议收藏」

{

线程 ManualResetEvent 类「建议收藏」
线程 ManualResetEvent 类「建议收藏」

ManualResetEvent mansig;

线程 ManualResetEvent 类「建议收藏」
线程 ManualResetEvent 类「建议收藏」

mansig = new ManualResetEvent(false);

线程 ManualResetEvent 类「建议收藏」
线程 ManualResetEvent 类「建议收藏」

Console.WriteLine(“ManualResetEvent Before WaitOne”);

线程 ManualResetEvent 类「建议收藏」
线程 ManualResetEvent 类「建议收藏」

bool b = mansig.WaitOne(1000, true);

线程 ManualResetEvent 类「建议收藏」
线程 ManualResetEvent 类「建议收藏」

Console.WriteLine(“ManualResetEvent After WaitOne” + b);

线程 ManualResetEvent 类「建议收藏」
线程 ManualResetEvent 类「建议收藏」

Console.ReadLine();

线程 ManualResetEvent 类「建议收藏」
线程 ManualResetEvent 类「建议收藏」

}

线程 ManualResetEvent 类「建议收藏」
线程 ManualResetEvent 类「建议收藏」

}

线程 ManualResetEvent 类「建议收藏」
线程 ManualResetEvent 类「建议收藏」

}

上面的例子中,构造了false值的ManualResetEvent对象,布尔值False把ManualResetEvent对象的初始状态设置为无信号。接着调用基类WaigHandle的WaitOne()方法。程序块在WaitOne()方法中暂停一秒,然后因为超时而退出。ManualResetEvent的状态仍然是False,因而WaitOne()返回的布尔值b是False。

下面的例子把有信号改为无信号,调用ReSet()方法,Set()方法。

[csharp] view plain copy

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Threading;
  5. namespace 同步
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. ManualResetEvent mansig;
  12. mansig = new ManualResetEvent(true);
  13. bool state = mansig.WaitOne(9000, true);
  14. Console.WriteLine(“ManualResetEvent After WaitOne” + state);
  15. mansig.Reset();
  16. state = mansig.WaitOne(9000, true);
  17. Console.WriteLine(“ManualResetEvent After WaitOne” + state);
  18. mansig.Set();
  19. state = mansig.WaitOne(9000, true);
  20. Console.WriteLine(“ManualResetEvent After WaitOne” + state);
  21. }
  22. }
  23. }

在ManualReset中,MnualTResetEvent对象的构造函数将其状态设置为有信号(true),结果,线程不在第一个

WaitOne()方法中等待,并返回True值。接着,ManualResetEvent对象的状态重新设置为无信号的(false),于是线程在超时之前必须等待5秒,调用Set()方法后也不用等待。

下面在看一个程序:

[csharp] view plain copy

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Threading;
  5. namespace 同步实验
  6. {
  7. class Program
  8. { ///
  9. /// ManualResetEvent建立时是把false作为start的初始状态,这个类用于通知另一个线程,让它等待一个或多个线程。
  10. /// 如这个例子中,等待线程thread1线程调用mre.WaitOne(), 用于发信号的线程调用mre.Set().
  11. ///
  12. public static ManualResetEvent mre = new ManualResetEvent(false);
  13. public static void trmain()
  14. {
  15. Thread tr = Thread.CurrentThread;
  16. Console.WriteLine(tr.Name + ” 开始第一波等待”);
  17. mre.WaitOne(); //等到什么时候呢?等到mre.Set()被调用
  18. Console.WriteLine(tr.Name + ” 第一波启动t”);
  19. mre.Reset(); //再次重置
  20. Console.WriteLine(tr.Name + ” 开始第二波等待”);
  21. mre.WaitOne(); //再次等待
  22. Console.WriteLine(tr.Name + ” 第二波启动”);
  23. for (int x = 0; x < 10; x++)
  24. {
  25. Thread.Sleep(1000);
  26. Console.WriteLine(tr.Name + “: ” + x);
  27. }
  28. }
  29. static void Main(string[] args)
  30. {
  31. Thread thrd1 = new Thread(new ThreadStart(trmain));
  32. thrd1.Name = “thread1”;
  33. thrd1.Start();
  34. Thread thrd2 = new Thread(new ThreadStart(trmain));
  35. thrd2.Name = “thread2”;
  36. thrd2.Start();
  37. for (int x = 0; x < 10; x++)
  38. {
  39. Thread.Sleep(900);
  40. Console.WriteLine(“Main :” + x);
  41. if (5 == x)
  42. {
  43. mre.Set(); //子线程的mre.WaitOne()可以执行了。第一次等待进程
  44. //; //如果什么都不做呢,mre.Wait()那个线程就一直等在那里了?
  45. }
  46. }
  47. while (thrd1.IsAlive)
  48. {
  49. Thread.Sleep(1000);
  50. Console.WriteLine(“Main: waiting for thread to stop…”);
  51. mre.Set(); //第二次通知等待进程
  52. }
  53. }
  54. }
  55. }

下面在看一个关于ManualResetEvent waitany的程序:

waitany一直等到有信号 才开始执行下面的语句.

[csharp] view plain copy

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Threading;
  5. namespace 同步实验
  6. {
  7. class Program
  8. {
  9. public static ManualResetEvent m_eventTemporaryTrigger;
  10. public static ManualResetEvent m_eventQuitTemporary;
  11. private static string m_triggerParam;
  12. public static void TemporaryConnThreadTrigger(String param)
  13. {
  14. m_triggerParam = param;
  15. m_eventTemporaryTrigger.Set();
  16. }
  17. static void Main(string[] args)
  18. {
  19. m_eventTemporaryTrigger =new ManualResetEvent(true);
  20. m_eventQuitTemporary = new ManualResetEvent(true);
  21. //m_eventQuitTemporary = new ManualResetEvent(false);
  22. m_eventTemporaryTrigger.Reset(); //临时线程处于工作之中时,m_eventTemporaryTrigger始终处于被触发状态
  23. // Trigger.strmain();
  24. WaitHandle[] events = new WaitHandle[2];
  25. events[0] = m_eventQuitTemporary;
  26. events[1] = m_eventTemporaryTrigger;
  27. //等待退出信号事件或触发事件
  28. // 返回结果:
  29. // 满足等待的对象的数组索引。
  30. int index = WaitHandle.WaitAny(events);
  31. Console.WriteLine(“run no wait”);
  32. if (index == 0)
  33. { //如果是将Main(),第三行置为false则不执行下面语句
  34. Console.WriteLine(“m_eventQuitTemporary run”);
  35. Console.WriteLine(m_triggerParam);
  36. }
  37. }
  38. }
  39. public class Trigger
  40. {
  41. public static void strmain()
  42. {
  43. Program.TemporaryConnThreadTrigger(“m_eventQuitTemporary run show”);
  44. }
  45. }
  46. }

要注意的是ManualResetEvent和AutoResetEvent 的构造函数都有一个bool的参数,用这个参数可以指定初始情况下,同步对象的处于阻塞(设置为false)还是非阻塞(设置为true)的状态。 另外WaitOne方法也可以带两个参数: WaitOne (int millisecondsTimeout,bool exitContext) millisecondsTimeout:等待的毫秒数,或为 Timeout.Infinite (-1),表示无限期等待。 exitContext:为 true,则等待之前先退出上下文的同步域(如果在同步上下文中),然后在稍后重新获取它;否则为false。 就是说,等待是可以加上一个期限的,如果等待的同步对象一直都不Set()的话,那么程序就会卡死,所以在WaitOne方法里面可以放置一个时间期限,单位是毫秒。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/158637.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年7月1,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档