Threading (thread), thread pool (ThreadPool) technology

Source: Internet
Author: User

Thread: is the smallest unit of the Windows Task Scheduler. A thread is an execution flow in a program, each with its own proprietary register (stack pointers, program counters, etc.), but the code area is shared, that is, different threads can execute the same function, and in one application it is often necessary to use multiple threads to handle different things, which can improve the efficiency of the program. Does not cause the main interface to appear unresponsive. Here is the main introduction to threading (thread), thread pool (ThreadPool) The difference between two different creation threads

In the usual case, when we need to open a new thread, we go straight through thread (inherited from System.Threading;) to create a new thread, there is a benefit that we can get the newly created thread, and we can do some related operations on this thread. But by creating a new thread that consumes resources, we all know that the resources of the operating system are limited, and when the user accesses more, the thread that needs to be opened is more, and when the thread that is opened exceeds the maximum number of threads in the system, the system is prone to crash. Let's take a look at the time required to create 100 threads

 for (int0; i++) // Create 100 Threads             {                new Thread (() =                    {                        Console.WriteLine ("  The ID of the creation thread is:"+Thread.CurrentThread.ManagedThreadId);                    }). Start ();            }            Sw. Stop ();            Console.WriteLine (" create 100 Threads Execution time is:"+sw.) Elapsedmilliseconds.tostring ());

After running we found that the time to create 100 threads was 489ms, and each thread was different. When the number of threads created exceeds the maximum number of threads in the system, the system is directly over. There is no solution, the answer is of course, is the thread pool (ThreadPool) technology.

By creating a thread pool object, when you need to use threads, go to the pool to see if there are any created threads, or simply take the created thread out of the thread pool, create a new thread, and when the thread is finished, it will be put back into the thread pool for the next use. This not only reduces the time spent creating threads and the memory consumed, but also increases the utilization of threads.

 for (int0; i++)            {                ThreadPool.QueueUserWorkItem (new waitcallback (s) = {                    Console.WriteLine ( " the ID of the creation thread is: " + Thread.CurrentThread.ManagedThreadId);                }));            }

From the running results above we can find that threads that are created through the thread pool, or the more accurate point should be to go to the thread pool to get the line turndown the time it takes to create the thread is really bad, the thread's ID is also duplicated, which is also the thread pool to get threads that can improve the utilization of threads. One thing to note here is that the thread that is fetched through the thread pool Cheng Tume the background thread

  Finally, a simple introduction to task work task items, task for. Net4.0 introduces a new thread pooling technique, as described in Microsoft's MSDN: asingle operation that represents a task class does not return a value, usually executed asynchronously. the Task object is a central idea for the first time in the. NET Framework 4, based on the asynchronous programming pattern of tasks.

A new thread pool work task item that is opened through the factory will be executed directly, and a new fly task item created by itself needs to be manually opened for execution

//Use the factory to create a work task item and execute theTask T1 = Task.Factory.StartNew (() = {                 //Execute Code            });//Task T1 = new Task (() =//    {            //Thread.Sleep (2000); //Console.WriteLine ("Current thread is:" +thread.currentthread.managedthreadid); //    }); //t1. Start ();//need to start manually to perform

Summary: In the actual development, when the thread does not need to operate, do not manually create threads, try to use thread pool technology to create a thread, you can improve the efficiency of the use of thread pooling technology can also improve the efficiency of the application.

Threading (thread), thread pool (ThreadPool) technology

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.