C # self-implemented thread pool function (1)

Source: Internet
Author: User

C # self-implemented thread pool function (1)
The technical background of the thread pool in Object-Oriented Programming, it takes a lot of time to create and destroy objects because it is necessary to obtain memory resources or other resources to create an object, therefore, one way to improve service program efficiency is to minimize the number of times objects are created and destroyed, especially the creation and destruction of resource-consuming objects. How to use existing objects to serve is a key problem that needs to be solved. In fact, this is the reason why some "pooled resources" technologies are generated. For example, the familiar database connection pool follows this idea. The thread pool technology introduced in this article is also in line with this idea.
How does thread pool technology improve the performance of server programs?

I mentioned that server programs refer to programs that can accept client requests and process requests, not just network server programs that accept requests from network customers.

Multithreading technology mainly solves the problem of multiple threads in a processor unit. It can significantly reduce the idle time of the processor unit and increase the throughput of the processor unit. However, improper multi-threaded application will increase the processing time for a single task. Here is a simple example:

Assume that the time for completing a task on a server is T

T1 thread creation time T2 thread execution time, including the time required for Inter-thread synchronization T3 thread destruction time

Obviously T = T1 + T2 + T3. Note that this is an extremely simplified assumption.

We can see that T1 and T3 are the overhead of multithreading itself. We are eager to reduce the time consumed by T1 and T3, thus reducing T time. But some threads do not notice this, So threads are frequently created or destroyed in the program, which leads to a considerable proportion of T1 and T3 in T. Obviously this highlights the thread's weakness (T1, T3), rather than the advantage (concurrency ).

The thread pool technology focuses on how to shorten or adjust T1 and T3 time to improve the performance of server programs. It arranges T1 and T3 in the start and end time periods or some idle time periods of the server program, so that when the server program processes customer requests, there will be no overhead of T1 and T3.

The thread pool not only adjusts the time periods generated by T1 and T3, but also significantly reduces the number of created threads. Let's look at an example:

Assume that a server processes 50000 requests a day, and each request requires a separate thread. We compare the total number of threads produced when the server that uses the thread pool technology and is not conducive to the thread pool technology processes these requests. In the thread pool, the number of threads is generally fixed, so the total number of threads generated will not exceed the number or upper limit of threads in the thread pool (hereinafter referred to as the thread pool size ), if the server does not use the thread pool to process these requests, the total number of threads is 50000. Generally, the thread pool size is much smaller than 50000. Therefore, the server program that uses the thread pool does not waste time processing to create 50000 requests, thus improving efficiency.

These are assumptions and cannot fully explain the problem. Next I will discuss the simple implementation of the thread pool and compare and test the program to illustrate the advantages of thread technology and application fields.

Simple implementation and Comparison Test of Thread Pool

A simple thread pool generally contains at least the following components.

  1. ThreadPoolManager: used to create and manage a thread pool.
  2. WorkThread: thread in the thread pool
  3. Task: A required interface for each Task to be executed by a worker thread.
  4. Task queue: used to store unprocessed tasks. Provides a buffer mechanism.
    Next I will demonstrate a simple thread pool. No optimizations were made.
    Using System; using System. collections. generic; using System. linq; using System. text; using System. collections; using System. threading; namespace ThreadManager {public class ThreadPoolManager {private int MaxThreadNum; private int MinThreadNum; private int GrowStepNum; // Number of threads public int ThreadNum {get; set ;} // default thread count public int defathreadthreadnum {get; set;} private Queue
       
        
    TaskQueue; private Queue
        
         
    WorkThreadList; public ThreadPoolManager (int I) {TaskQueue = new Queue
         
          
    (); WorkThreadList = new Queue
          
           
    (); DefaultThreadNum = 10; if (I> 0) DefaultThreadNum = I; CreateThreadPool (I);} public ThreadPoolManager (): this (10) {} public bool IsAllTaskFinish () {return TaskQueue. count = 0;} public void CreateThreadPool (int I) {if (WorkThreadList = null) WorkThreadList = new Queue
           
            
    (); Lock (WorkThreadList) {for (int j = 0; j <I; j ++) {ThreadNum ++; WorkThread workthread = new WorkThread (ref TaskQueue, ThreadNum ); workThreadList. enqueue (workthread) ;}} public void AddTask (Task task) {if (task = null) return; lock (TaskQueue) {TaskQueue. enqueue (task);} // Monitor. enter (TaskQueue); // TaskQueue. enqueue (task); // Monitor. exit (TaskQueue);} public void CloseThread () {// Objec T obj = null; while (WorkThreadList. Count! = 0) {try {WorkThread workthread = WorkThreadList. Dequeue (); workthread. CloseThread (); continue ;}catch (Exception) {} break ;}}}}
           
          
         
        
       
    Worker thread
    Using System; using System. collections. generic; using System. linq; using System. text; using System. threading; namespace ThreadManager {public class WorkThread {public int ThreadNum {get; set;} private bool flag; private Queue
       
        
    TaskQueue; private Task task; public WorkThread (ref Queue
        
         
    Queue, int I) {this. taskQueue = queue; ThreadNum = I; flag = true; new Thread (run ). start ();} public void run () {while (flag & TaskQueue! = Null) {// obtain the task lock (TaskQueue) {try {task = TaskQueue. dequeue ();} catch (Exception) {task = null;} if (task = null) continue;} try {task. setEnd (false); task. startTask ();} catch (Exception) {}try {if (! Task. isEnd () {task. setEnd (false); task. endTask () ;}} catch (Exception) {}} // end of while} public void CloseThread () {flag = false; try {if (task! = Null) task. EndTask () ;}catch (Exception ){}}}}
        
       
    Task class and implementation class
    Using System; using System. Collections. Generic; using System. Linq; using System. Text; namespace ThreadManager {public interface Task {////// Set flag of task .///Void SetEnd (bool flag );////// Start task .///Void StartTask ();////// End task .///Void EndTask ();////// Get status of task .//////
       Bool IsEnd () ;}} using System; using System. collections. generic; using System. linq; using System. text; using System. threading; namespace ThreadManager {public class TestTask: Task {private bool is_end; public void SetEnd (bool flag) {is_end = flag;} public void StartTask () {Run ();} public void EndTask () {is_end = true; Console. writeLine (Thread. currentThread. managedThreadId + ":" + "end! ");} Public bool IsEnd () {return is_end;} public void Run () {for (int I = 0; I <1000; I ++) {Console. writeLine (Thread. currentThread. managedThreadId + ":" + I );}}}}


    The problem with this simple model is that, in many cases, the acquisition of tasks is constantly trying to reduce the performance. The method to be improved is to add a semaphore mechanism so that the program will not be idling!

    In the next article, I will optimize it so that the thread pool can truly improve efficiency!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.