Use threadpool instead of Thread

Source: Internet
Author: User

Thread space overhead

  1. The thread kernel object. Contains context information. 32-bit system occupies 700 bytes
  2. Thread environment block. Including thread Exception Handling links. 32-bit system occupies 4 kb
  3. User Mode stack. Save method parameters, local variables, and returned values
  4. Kernel Mode stack. When calling the kernel-mode function of the operating system, the system copies the function parameters from the user mode stack to the kernel mode stack. 32-bit system occupied by 12kb

Thread time overhead

  1. During the creation, the system initializes the above memory space one after another
  2. CLR loads DLL to the dllmain method and transmits the connection flag
  3. Thread context switch
    1. Enter Kernel Mode
    2. Save the context information to the executing thread kernel object.
    3. The system obtains a spinlock and determines the next thread to be executed. Release the spinlock. If the next thread is not in the same process, virtual address exchange is required.
    4. Load context information from the kernel object of the executed thread
    5. Exit Kernel Mode

When the thread pool is used, the CLR will not destroy this thread, but will keep this thread for a period of time.

using System;using System.Diagnostics;using System.Threading;namespace ConsoleApplication1{    class Program    {        static void Main(string[] args)        {            var p = new Program();            Stopwatch sw = new Stopwatch();            sw.Start();            p.Thread();            sw.Stop();            Console.WriteLine(sw.ElapsedTicks);            sw.Restart();            p.Pool();            sw.Stop();            Console.WriteLine(sw.ElapsedTicks);            Console.ReadKey();                    }        void Thread()        {            for (int i = 0; i < 10; i++)            {                var worker = new Thread(() =>                {                    //Console.WriteLine("Thread Do");                });                worker.Start();            }        }        void Pool()        {            for (int i = 0; i < 10; i++)            {                ThreadPool.QueueUserWorkItem(state =>                {                    //Console.WriteLine("Pool Do");                });            }        }    }}


Use threadpool instead of Thread

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.