C # Use AutoResetEvent to implement sequential thread execution
A friend asked me how to implement the sequential execution of threads a few days ago. To be honest, although I have read the CLR book, I read the thread twice. However, there is still no idea after this problem is solved. I accidentally saw the AutoResetEvent item again when searching materials today. Of course, I know it is related to the thread and used to handle thread switching (it may be wrong before testing the Demo ), so I decided to use AutoResetEvent to solve the above problem. Here is an example of a garden friend in the park. This example is the process of buying books -- Payment -- and getting books. This process will last n times (set through variables, and every time it is executed in order, there may be questions from fellow students, just Sleep. Why do we have to have multiple threads? If it takes too long to process a link or the business is complicated, then the entire program will not respond directly, so here we add multithreading to ensure the program's response.
Class Program {// number of cycles const int numIterations = 10; // buyer's book static AutoResetEvent buyResetEvent = new AutoResetEvent (false); // payment static AutoResetEvent payResetEvent = new orautesetevent (false ); // obtain the static AutoResetEvent getBookEvent = new AutoResetEvent (false); // The number of cycles static int number; static void Main (string [] args) {// payment Thread payMoneyThread = new Thread (new ThreadStart (PayMoneyProc); payMoneyThread. name = "Payment Thread"; // Book Receiving Thread getBookThread = new Thread (new ThreadStart (GetBookProc); getBookThread. name = "Book Receiving thread"; payMoneyThread. start (); getBookThread. start (); for (int I = 1; I <= numIterations; I ++) {Console. writeLine ("book buying thread: quantity {0}", I); number = I; // allow the payment thread to wait for payResetEvent. set (); // The book-buying thread is prohibited from waiting for the buyResetEvent. waitOne ();} Console. read ();} static void PayMoneyProc () {while (true) {// wait for payment payResetEvent. waitOne (); Console. writeLine ("{0}: quantity {1}", Thread. currentThread. name, number); // allows the Book Receiving thread to wait for getBookEvent. set () ;}} static void GetBookProc () {while (true) {// wait for the payment getBookEvent. waitOne (); Console. writeLine ("{0}: quantity {1}", Thread. currentThread. name, number); Console. writeLine ("--------------------------------------"); // allows the book-buying thread to wait (by this step, a complete book-buying process is completed, and the book-picking process is complete) buyResetEvent. set ();}}}
The above code is not complex, but there are several key objects and methods, which will be described in detail later. 1. the AutoResetEvent for books, payment, and books are defined respectively. Note that false is input during the definition, which means that AutoSet is unavailable by default. You need to manually call the Set method. This object is the key to determining whether a request can be WaitOne, when AutoResetEvent. set execution can use AutoResetEvent. waitOne makes a waiting request. If there is another WaitOne request, it will continue to wait for the execution of the Set; 2. first, let's look at the for loop. in the loop, we use payResetEvent. the Set () Code, which also allows the WaitOne request in PayThread to be approved (as described below), and the buyResetEvent is added. waitOne () (in this way, the new process cannot be executed before the last purchase process ends, which is our biggest problem and ensures that the thread is executed in order); 3. defines the payment and book-taking Thread, and is a While loop in the method. This loop is mainly used to perform our process multiple times. After all, the Thread will only execute it once. Of course, this is not the focus. The focus is on the While operation. First, let's look at the PayThrea operation. First, call payResetEvent. waitOne () requests a waiting operation. Of course, it can be executed immediately, because in 2 we said that payResetEvent is called in. set () operation to directly obtain a request response, output key information, and then focus on it. We call getBookEvent. set (). Why is that? Because the payment is unsuccessful, you cannot take the books. That is, theft. Note, attention, and attention. If it is important to say three times, the GetBookThread operation also has a waitOne request, but why not execute it because there is no Set, when we initialize AutoResetEvent, we Set it to false. By default, we cannot get a request. We must call Set manually. Because getBookEvent is called in the last line of PayMoneyThread code. set (), so getBookEvent. waitOne is available. In the GetBookThread, the key information is output sequentially, And the last line of code is returned. An AutoResetEvent is called again. set (). That's right. It's the object of buying books, because the complete process of buying books in this step is over. You can buy books again. Then return to the last line of code in the for loop again. buyResetEvent. Wait () is now revived and the book buying process starts several times.