Maximum number of concurrent threads that control each thread pool

Source: Internet
Author: User

"I original ", Welcome to exchange and fractal technology, reproduced please attach the following content:

Author: itshare "Turn from"

1. Purpose of the experiment:

When using the thread pool, it is sometimes necessary to consider the maximum number of threads for the server and the best trade-offs for the program to perform all business logic.
Not more logical threads, and the new logical thread must wait in the thread pool's waiting queue until the thread that is working in it is finished executing.
Only the system thread takes out the logical thread in the wait queue for CPU operation.

2. Problem solving:
<a> without taking into account the maximum number of parallel threads that the server can actually support, the program continues to request new logical threads from the thread pool, and this time we can see that CPU usage is soaring, and that memory, network bandwidth consumption increases as logical threads accumulate in the CPU queue.
<b> if we want to have 200 HTTP network traffic in the main program to be executed, how to process 10 network HTTP communication calls concurrently with 10 threads per loop, the next loop will be executed only after the 10 threads of the previous loop have finished executing, and the main program listens and waits for 200 HTTP network traffic to be executed in the CPU thread pool before exiting the main program.

3. Implementation logic:
We implemented each of the two AutoResetEvent and Thread listener monitor:
<a>wait_sync: Concurrent execution of the task thread, each loop handles only the maximum of 10 threads, each of which makes HTTP communication to the network. The next loop is processed only after the 10 threads of the current loop have finished executing.
<b> Wait_main: The main program thread listens and waits, and the main program thread exits the program only after all the 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) = Occupy lock that consumes shared thread resources, Monitor.Exit (List_thread) frees the shared thread resource for the seizure lock.
<d> N_total_thread: Use with Wait_main, record all logical threads, the current total number of executions completed, to determine whether the main thread still need to continue to wait, or can end the main program run.

4. Main code:
<a> thread pool control code, as follows:

        <summary>///Multithreading call WCF//</summary>//<param name= "Select" > Call to WCF Type,1=restful,2=tcp</param>//<param name= "num" ></param> static void Dotest_multithread (ST Ring Select, long num) {int n_max_thread = 10;//Set parallel to a maximum of 10 threads int n_total_thread = 0;//To            Control: The end of the main program executes when all task threads are 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 ("Invoke WCF in the same way = {0}, 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 (fals e);            Used to control: the end execution of the main program, when all task threads have finished executing 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 the first Max thread has not started {//log_wait. Info (String.                    Format ("thread n= {0},for i= {1}", Dic_thread.count, i + 1)); Wait_sync. WaitOne (); The next concurrent thread} Log_for is sent after each concurrent 10 threads, waiting for processing to complete. Info (String.                Format ("thread n= {0},for i= {1}", List_thread.count, i + 1));                        System.Threading.ThreadPool.QueueUserWorkItem (data) = {                         int id = System.Threading.Thread.CurrentThread.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 mode call {Query_h                        TTY ();                        } else {query_socket ();                        } N_total_thread + = 1; if (List_thread.count = = (N_max_thread) | | n_total_thread = = num) {lis                            T_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 execution}          else {Wait_main. Set ();            Main program thread, continue execution}}}, List_thread); } wait_main.            WaitOne (); Console.WriteLine (String. Format ("Total test {0} times, total time of {1}, Average time {2}", Num, ( ToString (), (            Totalmilliseconds/num));        Query_thread (); }


<b> WCF Backend 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 Num = 0;  Number of threads private static Object lock_num = 0; Share data-lock///<summary>//Online application serial number///</summary>//<returns></returns > [WebGet (uritemplate = "Getseqnum/json", Responseformat = Webmessageformat.json)] public string Getseqnu                Mber () {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);           }} 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 on demand) thread concurrently execution, and the main program exits the main program after all requests have been executed.

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

Thread Id/thread Num

2. WCF logs: Logfile\seqservice\info

Thread Id/thread Num

Maximum number of concurrent threads that control each thread pool

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: 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.