We can use lock, mutex to ensure that shared resources are properly operated, but when multiple threads need to communicate with each other, such as when thread A is finished to tell the thread that b,b is doing it,
This is what we should do with the thread event. NET provides the AutoResetEvent and ManualResetEvent two classes to handle.
When a thread requires exclusive resources, Autoresetevent.waitone () is used to wait for the resource, and if AutoResetEvent is non-signaled, the thread blocks and waits for other threads to release the resource. Other threads
When the resource is exhausted, the resource is freed by calling the set method, which notifies the waiting thread that the resource is available. The first waiting thread receives a signal and then consumes the resource to continue execution, and when the thread finishes executing, the AutoResetEvent is automatically set
To terminate the status.
AutoResetEvent is initialized with parameters to set the initialization state, true indicates a terminating state, and False indicates a non-terminating state.
Let's look at an example.
Divide two people to complete tree planting, a is responsible for digging the pit, B is responsible for moving saplings and watering. The code is implemented as follows:
Public classThreadeventtest {Private StaticAutoResetEvent AutoResetEvent =NewAutoResetEvent (false); Public Static voidPlanttree () {//Digging PitsThread Shovelthread =NewThread (NewThreadStart (shovel)); Shovelthread.name="A"; Shovelthread.start (); Thread.Sleep ( -); //Planting Trees wateringThread Warterthread =NewThread (NewThreadStart (Wartertree)); Warterthread.name="B"; Warterthread.start (); } Private Static voidShovel () {Console.WriteLine ("Thread {0} starts digging holes", Thread.CurrentThread.Name); Console.WriteLine ("Thread {0} Dug pit completed, can grow tree watering", Thread.CurrentThread.Name); AutoResetEvent.Set (); } Private Static voidWartertree () {Console.WriteLine ("Thread {0} starts carrying saplings and preparing water", Thread.CurrentThread.Name); Console.WriteLine ("Thread {0} has planted saplings and water ready to be planted with trees after the pit is finished.", Thread.CurrentThread.Name); Autoresetevent.waitone (); Console.WriteLine ("Thread {0} start planting tree watering", Thread.CurrentThread.Name); Thread.Sleep ( +); Console.WriteLine ("Thread {0} tree watering complete", Thread.CurrentThread.Name); //Console.WriteLine ("Complete Tree Planting"); } }
The main thread turns on two threads A, b to complete digging pits and watering respectively. A thread will perform a dig-out task when it is open, and the B thread will execute synchronously, but when the B thread executes the value WaitOne, because the AutoResetEvent state we initialize is non-terminating, the B thread must wait for the set signal, only if the a thread executes and set , that is, a thread dug pit completed, B thread can be planted to water, thus guaranteeing the entire
Tree planting work is done in an orderly manner.
The AutoResetEvent state becomes signaled after set, and when WaitOne executes, it automatically resets the state to a non-terminating state without calling Reset (), which is the difference between the ManualResetEvent and the.
ManualResetEvent must be reset after set to reset the state, the call needs to appear in pairs, in order to ensure that the WaitOne blocking thread, so that the thread orderly execution down.
Event of thread synchronization