C # Parallel Programming----Threadpool

Source: Internet
Author: User

Brief introduction:

The creation of a managed thread requires thousands of CPU cycles, and there is a noticeable overhead when a thread switch occurs. It is a way to improve the efficiency of the system to consider the reuse of threads and avoid repeatedly creating new threads.

The thread pool is an efficient way to create threads that are managed by the thread pool engine, and the developer will encapsulate the business or operations that need to be handled into a "work item" work item that is passed to the thread pool queue, picked and executed by different worker threads in the thread pools. When execution is complete, the thread is not destroyed, but continues to select other work items.

Code:

usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.Threading;namespacesample8_1_threadpool_basic{classProgram { Public Static voidWork1 () {System.Console.WriteLine ("ThreadWork1 Run {");  for(inti =0; I < -; i++) {System.Console.WriteLine ("THREADWORK1 * * * *:"+i); } System.Console.WriteLine ("ThreadWork1 Run}"); }         Public Static voidWork2 () {System.Console.WriteLine ("ThreadWork2 Run {");  for(inti =0; I < -; i++) {System.Console.WriteLine ("THREADWORK2 =====:"+i); } System.Console.WriteLine ("ThreadWork2 Run}"); }        Static voidMain (string[] args) {ThreadPool.QueueUserWorkItem (stat)={Work1 ();            }); ThreadPool.QueueUserWorkItem (STAT)={WORK2 ();            }); intA; intb; Threadpool.getminthreads ( outA outb); Console.WriteLine ("a="+a+"; b="+b);        Console.ReadLine (); }    }}

C # Parallel Programming----Threadpool

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.