In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to use AutoResetEvent to control threads, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can get something.
AutoResetEvent concept
The AutoResetEvent object is used for thread synchronization operations, and the AutoResetEvent class inherits the waitHandle class. The waitOne () method inherits from the waitHandle class.
The AutoResetEvent object has two states of termination and non-termination. The termination state is that the thread continues to execute, and the non-termination state blocks the thread. You can call the set and reset methods to make the object enter the termination and non-termination state. -"it's easy to understand that if the AutoResetEvent object is in a terminated state, it's like leaving others alone and letting you go wild (waitOne () gets wild signals)
As the name implies, the object of AutoResetEvent will automatically call reset after calling set once, and enter the non-terminating state to make the thread that called the waiting method enter the blocking state. -"it's easy to understand that if the AutoResetEvent object is in a non-terminating state, it starts to manage others, and waitOne () gets a signal that stays where it is.
The waitone of the waitHandle object puts the current thread into a blocking state and waits for a signal. Execution does not continue until the current waitHandle object receives a signal.
Set can send a signal that allows a thread to call waitone while waiting for the thread to continue execution. The set method of ManulResetEvent can allow multiple. But to close it manually, call reset ()
Reset can put waiting threads into a blocking state because of a call to waitone ().
The main methods of AutoResetEvent are
AutoResetEvent (bool initialState): constructor that initializes a new instance of the class with a Boolean value indicating whether the initial state is set to a terminated Boolean value. False: no signal, the WaitOne method of the child thread will not be called automatically true: if there is a signal, the WaitOne method of the child thread will be called automatically
Reset (): sets the event state to a non-terminating state, causing threads to block; if the operation is successful, return true; otherwise, return false.
Set (): sets the event state to the terminated state, allowing one or more waiting threads to continue; if the operation is successful, return true; otherwise, return false.
WaitOne (): blocks the current thread until a signal is received.
WaitOne (TimeSpan, Boolean): blocks the current thread until the current instance receives a signal, measures the time interval using TimeSpan and specifies whether to exit the synchronization domain before waiting.
With the above explanation, start showing the code
/ / true if you want to set the initial state to terminate To set the initial state to non-terminating, false static AutoResetEvent oddResetEvent = new AutoResetEvent (false); static AutoResetEvent evenResetEvent = new AutoResetEvent (false); static int i = 0; static void Main (string [] args) {/ / ThreadStart is a delegate Thread thread1 = new Thread (new ThreadStart (show)); thread1.Name = "even thread" Thread thread2 = new Thread (new ThreadStart (show)); thread2.Name = "odd thread"; thread1.Start (); Thread.Sleep (200); thread2.Start (); Console.Read ()
} public static void show () {if (I = = 1) evenResetEvent.WaitOne (); while (I)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.