Get to know the thread pool (a)

Creating a thread is a very costly operation, each asynchronous operation to create a thread will have a significant impact on performance CPU. To solve this problem we introduced the concept of the thread pool, so-called thread pool that we allocate some resources to advance these resources in the resource pool, using every time you need to use a from the inside out, came alive again after use go back. Thread pool for general use in the need to create a large number of short-lived and large overhead in resources. .NET thread pool is located in System.Threading.ThreadPool class, which accepts CLR management. ThreadPool class has a QueueUserWorkItem method is a static method. It accepts a delegate, representing the user-defined asynchronous operation. After the method is called, will be commissioned into the internal queue. If the pool does not have any thread, will create a new Worker Thread (worker thread) and put in the queue first delegate to the Work Thread in.
One thing to note here, when a new operation is added to the thread pool, if the previous operation is completed, then the new operation will reuse threads to execute. But if the new operation is added too much too fast thread pool, the thread pool will create more threads to perform the operation. Then the number of threads created is limited, after the limit is reached, the operation will add to the mix after waiting thread is returned to the thread pool and the ability to execute them in the queue. When there is no operation into the thread pool, thread pool would be freed over the expiration time of the thread, in order to reduce the pressure Caozuoxitong and CPU.

Tip:

  1. Must not be put into operation in the thread pool or into the long-running operation will block the thread, which can cause serious performance problems and inexplicable bug.
  2. All threads in the pool are background threads, when all foreground threads in your application complete background thread will stop working, even if it has not completed the work done.

APM zero, the thread pool and commission

APM is a so-called asynchronous programming model, he is a mode that allows the operator to do more with fewer threads, .NET Framework class also implements many of the patterns. We may also be implemented to return a custom class of type BeginXXX methods and methods EndXXX IAsyncResult interface. Delegate types also defines BeginInv

Guess you like

Origin blog.csdn.net/gangzhucoll/article/details/103652075