C # Implementation Control thread pool maximum number concurrent thread _c# tutorial

Source: Internet
Author: User
Tags cpu usage

1. Experimental purposes:

When using a thread pool, it is sometimes necessary to consider the maximum number of threads for the server and the fastest execution of all business logic by the program.
The more logical threads are not the better, and the new logical threads must wait in the queue waiting in the thread pool until the thread that is working in the threads sink executes.
The system thread takes out the logical threads in the waiting queue for CPU operations.

2. Problem solving:

<a> without taking into account the maximum number of parallel threads that the server actually supports, the program keeps requesting new logical threads from the thread pool, and this time we can see that CPU usage is soaring, and that memory and network bandwidth usage are increasing as the logical threads accumulate in the CPU queue.

<b> if we want 200 HTTP network traffic to be performed in the main program, how to process 10 network HTTP traffic each time with 10 threads, the next loop will only execute the next loop only after the 10 threads in the last loop have finished executing. and the main program listens and waits until 200 HTTP network traffic is executed in the CPU thread pool before exiting the main program.

3. Implementation logic:

We implemented two AutoResetEvent and thread Listener monitor, respectively:

<a>wait_sync: Concurrent execution of the task thread, which handles only the maximum 10 threads per loop for HTTP communication to the network. And the 10 threads of the current loop are executed before the next round of processing occurs.
<b> Wait_main: The main program thread listens and waits, the main program thread exits the program only after all task threads have finished executing.
<c> List_thread: Responsible for recording the number of system threads that the CPU actually allocates per cycle. In conjunction with Monitor, Monitor.Enter (list_thread) = occupies a lock on the shared thread resource, Monitor.Exit (List_thread) frees up the shared thread resource's usage lock.
<d> N_total_thread: With Wait_main use, record all logical threads, the current total number of completed, used to determine whether the main thread still need to wait, or can end the main program run.

4. Main code:

<a> thread pool control code, as follows:

<summary>///Multithreading calls WCF///</summary>///<param name= "Select" > Invoking WCF, 1=restful,2=tcp</ param>///<param name= "num" ></param> static void Dotest_multithread (string select, Long num) {int N_max _thread = 10; Set parallel maximum of 10 threads int n_total_thread = 0;
  Used to control: the end execution of the main program, when all task threads have been executed ILog log_add = new Loghelper ("Add_thread");
  ILog Log_del = new Loghelper ("Del_thread");
  ILog log_wait = new Loghelper ("Wait_thread");
  ILog log_set = new Loghelper ("Set_thread");
 
  ILog log_for = new Loghelper ("For_thread"); Console.title = string. Format ("The way to invoke WCF => {0}, the number of calls => {1}", select = = "1"?)
   
  "Restful": "Socket", num);
 
  list<int> list_thread = new list<int> (); System.Threading.AutoResetEvent Wait_sync = new System.Threading.AutoResetEvent (false); Used to control: concurrent maximum number of threads =n_max_thread System.Threading.AutoResetEvent wait_main = new System.Threading.AutoResetEvent (false) ; Used to control: the end execution of the main program, when all task threads have completed DateTime date_sTEP = DateTime.Now;
    for (long i = 0; i < num; i++) {num_query_static++; if (I >0 && (i+1-1)% N_max_thread = 0)//-1 indicates that Max thread has not started {//log_wait. Info (String.
      Format ("thread n= {0},for i= {1}", Dic_thread.count, i + 1)); Wait_sync. WaitOne (); Each concurrent 10 threads, waiting for processing, after sending the next concurrent thread} log_for. Info (String.
 
    Format ("thread n= {0},for i= {1}", List_thread.count, i + 1)); System.Threading.ThreadPool.QueueUserWorkItem (data) => {int id = System.Threading.Thread.Current
        Thread.managedthreadid;
        System.Threading.Monitor.Enter (List_thread);
        List_thread.add (ID);
 
        System.Threading.Monitor.Exit (List_thread); Log_add. Info (String. Format ("id={0}, Count={1}", ID, list_thread.count));
        Log if (select = = "1")//RESTful method calls {Query_htty ();
        else {query_socket ();
        } n_total_thread = 1; if (List_thread.count = = (N_maX_thread) | |
          N_total_thread = = num) {list_thread.clear (); Log_set. Info (String.
          Format ("thread n= {0},for i= {1}", Dic_thread.count, i + 1)); Wait_sync. 
          Set (); if (n_total_thread!= num) {Wait_sync. Set (); Task thread, continue to execute} else {Wait_main. Set ();
  Main program thread, continue execution}}, List_thread); } wait_main.
 
  WaitOne (); Console.WriteLine (String. Format ("Total test {0} times, total time-consuming {1}", average time consuming {2} ", Num, (datetime.now-date_step).) ToString (), (Datetime.now-date_step).
 
  Totalmilliseconds/num));
Query_thread ();
 }

<b> WCF background Service code

 private static ILog log = new Loghelper ("Seqservice");/log private static Dictionary<int, datetime> dic_thread = new Dictionary<int, datetime> (); Thread list private static long = 0; Number of threads private static Object lock_num = 0;  Shared data-lock///<summary>///online application serial number///</summary>///<returns></returns> [WebGet (UriTemplate = "Getseqnum/json", Responseformat = Webmessageformat.json)] public string Getseqnumber () {lock (lock_num) {num+
    +;
    int id_thread = System.Threading.Thread.CurrentThread.ManagedThreadId;
    DateTime now = DateTime.Now; if (!dic_thread. TryGetValue (Id_thread, out now)) {Dic_thread.
    ADD (Id_thread, DateTime.Now); 
 
  The string ret = DateTime.Now.ToString ("yyyyMMdd") + num.tostring (new string (' 0 ', 9)); Log. Info (String. Format ("{0}, thread={1}/{2}", ret, System.Threading.Thread.CurrentThread.ManagedThreadId, Dic_thread.)
  Count));
return ret; } 

5. Experimental results

1.10,000 WCF network HTTP requests, the CPU is divided into 10 (10 can be adjusted by demand) thread concurrent execution, and the main program after all requests are executed, before exiting the main program.

1. Front-end log: Logfile\add_thread\info

2. WCF log: Logfile\seqservice\info

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.