As you can see, in the above routine, synchronization is completed by waiting for monitor. Pulse. First, the producer produces a value, while the consumer is in the waiting state at the same time until it receives the "pulse" notification from the producer that production has been completed, and then the consumer enters the consumption state, the producer will call monitor after waiting for the consumer to complete the operation. the "pulse" emitted by pulese ". The execution result is simple: 
 
 
 
 
  
   
   | Produce: 1 Consume: 1
 Produce: 2
 Consume: 2
 Produce: 3
 Consume: 3
 ...
 ...
 Produce: 20
 Consume: 20
 | 
 
  
 
 
In fact, this simple example has helped us solve multithreading applications.ProgramAs long as you understand the basic method for solving inter-thread conflicts, it is easy to apply it to complicated programs.
 
 
 
4. Thread Pool and timer-Automatic Management of Multithreading 
In multi-threaded programs, there are often two situations. In one case, the threads in the application spend most of the time waiting for the status, waiting for an event to happen before responding; in another case, the thread is usually in sleep state, but is periodically awakened. In the. NET Framework, we use threadpool to deal with the first case, and timer to deal with the second case.
 
 
The threadpool class provides a thread pool maintained by the system-a container that can be considered as a thread. This container must be supported by Windows 2000 and later versions, some of these methods call APIs that are only available in later versions of Windows. You can use the threadpool. queueuserworkitem () method to place the thread in the thread pool. The prototype of this method is as follows:
 
 
 
// Put a thread into the thread pool. The start () method of the thread calls the Function Represented by the waitcallback proxy object.
Public static bool queueuserworkitem (waitcallback );
// The reload method is as follows. The parameter object is passed to the method represented by waitcallback.
Public static bool queueuserworkitem (waitcallback, object );
 
 
 
It should be noted that the threadpool class is also a static class, and you cannot and do not need to generate its objects. Once you use this method to add a project to the thread pool, this project cannot be canceled. Here, you don't need to create a thread on your own. You just need to write the work you want to do as a function and pass it to threadpool as a parameter. the queueuserworkitem () method is used. The passed method relies on the waitcallback proxy object, and the thread creation, management, and running tasks are automatically completed by the system, you don't need to consider the complicated details. The advantages of the thread pool are shown here, as if you are the boss of the company-you just need to arrange jobs without having to do it yourself.