zoukankan      html  css  js  c++  java
  • C# System.Threading.AutoResetEvent

    表示线程同步事件在一个等待线程释放后收到信号时自动重置.

     1 using System;
     2 using System.Threading;
     3 
     4 // Visual Studio: Replace the default class in a Console project with 
     5 //                the following class.
     6 class Example
     7 {
     8     private static AutoResetEvent event_1 = new AutoResetEvent(true);
     9     private static AutoResetEvent event_2 = new AutoResetEvent(false);
    10 
    11     static void Main()
    12     {
    13         Console.WriteLine("Press Enter to create three threads and start them.
    " +
    14                           "The threads wait on AutoResetEvent #1, which was created
    " +
    15                           "in the signaled state, so the first thread is released.
    " +
    16                           "This puts AutoResetEvent #1 into the unsignaled state.");
    17         Console.ReadLine();
    18             
    19         for (int i = 1; i < 4; i++)
    20         {
    21             Thread t = new Thread(ThreadProc);
    22             t.Name = "Thread_" + i;
    23             t.Start();
    24         }
    25         Thread.Sleep(250);
    26 
    27         for (int i = 0; i < 2; i++)
    28         {
    29             Console.WriteLine("Press Enter to release another thread.");
    30             Console.ReadLine();
    31             event_1.Set();
    32             Thread.Sleep(250);
    33         }
    34 
    35         Console.WriteLine("
    All threads are now waiting on AutoResetEvent #2.");
    36         for (int i = 0; i < 3; i++)
    37         {
    38             Console.WriteLine("Press Enter to release a thread.");
    39             Console.ReadLine();
    40             event_2.Set();
    41             Thread.Sleep(250);
    42         }
    43 
    44         // Visual Studio: Uncomment the following line.
    45         //Console.Readline();
    46     }
    47 
    48     static void ThreadProc()
    49     {
    50         string name = Thread.CurrentThread.Name;
    51 
    52         Console.WriteLine("{0} waits on AutoResetEvent #1.", name);
    53         event_1.WaitOne();
    54         Console.WriteLine("{0} is released from AutoResetEvent #1.", name);
    55 
    56         Console.WriteLine("{0} waits on AutoResetEvent #2.", name);
    57         event_2.WaitOne();
    58         Console.WriteLine("{0} is released from AutoResetEvent #2.", name);
    59 
    60         Console.WriteLine("{0} ends.", name);
    61     }
    62 }
    63 
    64 /* This example produces output similar to the following:
    65 
    66 Press Enter to create three threads and start them.
    67 The threads wait on AutoResetEvent #1, which was created
    68 in the signaled state, so the first thread is released.
    69 This puts AutoResetEvent #1 into the unsignaled state.
    70 
    71 Thread_1 waits on AutoResetEvent #1.
    72 Thread_1 is released from AutoResetEvent #1.
    73 Thread_1 waits on AutoResetEvent #2.
    74 Thread_3 waits on AutoResetEvent #1.
    75 Thread_2 waits on AutoResetEvent #1.
    76 Press Enter to release another thread.
    77 
    78 Thread_3 is released from AutoResetEvent #1.
    79 Thread_3 waits on AutoResetEvent #2.
    80 Press Enter to release another thread.
    81 
    82 Thread_2 is released from AutoResetEvent #1.
    83 Thread_2 waits on AutoResetEvent #2.
    84 
    85 All threads are now waiting on AutoResetEvent #2.
    86 Press Enter to release a thread.
    87 
    88 Thread_2 is released from AutoResetEvent #2.
    89 Thread_2 ends.
    90 Press Enter to release a thread.
    91 
    92 Thread_1 is released from AutoResetEvent #2.
    93 Thread_1 ends.
    94 Press Enter to release a thread.
    95 
    96 Thread_3 is released from AutoResetEvent #2.
    97 Thread_3 ends.
    98  */

    构造函数 

    AutoResetEvent(Boolean)

    用一个指示是否将初始状态设置为终止的布尔值初始化 AutoResetEvent 类的新实例。

    Close()

    释放由当前 WaitHandle 占用的所有资源。

    (Inherited from WaitHandle)
    CreateObjRef(Type)

    创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。

    (Inherited from MarshalByRefObject)
    Dispose()

    释放 WaitHandle 类的当前实例所使用的所有资源。

    (Inherited from WaitHandle)
    Dispose(Boolean)

    当在派生类中重写时,释放 WaitHandle 使用的非托管资源,并且可选择释放托管资源。

    (Inherited from WaitHandle)
    Equals(Object)

    确定指定的对象是否等于当前对象。

    (Inherited from Object)
    GetHashCode()

    作为默认哈希函数。

    (Inherited from Object)
    GetLifetimeService()

    检索控制此实例的生存期策略的当前生存期服务对象。

    (Inherited from MarshalByRefObject)
    GetType()

    获取当前实例的 Type

    (Inherited from Object)
    InitializeLifetimeService()

    获取生存期服务对象来控制此实例的生存期策略。

    (Inherited from MarshalByRefObject)
    MemberwiseClone()

    创建当前 Object 的浅表副本。

    (Inherited from Object)
    MemberwiseClone(Boolean)

    创建当前 MarshalByRefObject 对象的浅表副本。

    (Inherited from MarshalByRefObject)
    ToString()

    返回表示当前对象的字符串。

    (Inherited from Object)
    WaitOne()

    阻止当前线程,直到当前 WaitHandle 收到信号。

    (Inherited from WaitHandle)
    WaitOne(Int32)

    阻止当前线程,直到当前 WaitHandle 收到信号,同时使用 32 位带符号整数指定时间间隔(以毫秒为单位)。

    (Inherited from WaitHandle)
    WaitOne(Int32, Boolean)

    阻止当前线程,直到当前的 WaitHandle 收到信号为止,同时使用 32 位带符号整数指定时间间隔,并指定是否在等待之前退出同步域。

    (Inherited from WaitHandle)
    WaitOne(TimeSpan)

    阻止当前线程,直到当前实例收到信号,同时使用 TimeSpan 指定时间间隔。

    (Inherited from WaitHandle)
    WaitOne(TimeSpan, Boolean)

    阻止当前线程,直到当前实例收到信号为止,同时使用 TimeSpan 指定时间间隔,并指定是否在等待之前退出同步域。

    (Inherited from WaitHandle)
    Handle

    获取或设置本机操作系统句柄。

    (Inherited from WaitHandle)
    SafeWaitHandle

    获取或设置本机操作系统句柄。

    (Inherited from WaitHandle)
    IDisposable.Dispose()

    释放由 WaitHandle 使用的所有资源。

    (Inherited from WaitHandle)
    WaitTimeout

    指示在任何等待句柄终止之前 WaitAny(WaitHandle[], Int32, Boolean) 操作已超时。此字段为常数。

    (Inherited from WaitHandle)
  • 相关阅读:
    Docker安装模拟MySQL集群
    Java中类似PHP的var_dump()
    同样的sql语句在数据库中能查出结果,但是在java springboot项目中,查不出来
    import org.junit.Test 导入失败 @Test JUnit4单元测试找不到
    解决用eclipse spring start project 自动创建项目时pom文件错误
    Laravel 队列的简单使用例子
    跨库触发器
    MySQL 主从复制 详细实例讲解 与 常见错误解决方法
    2014年末总结:对大数据处理的一点思考
    Range
  • 原文地址:https://www.cnblogs.com/hushaojun/p/10602030.html
Copyright © 2011-2022 走看看