Introduce multithreading in. NET (two-thread pool), and elaborate on. net

Source: Internet
Author: User
Tags thread logic

Introduce multithreading in. NET (two-thread pool), and elaborate on. net

In the previous chapter, we learned that thread creation and destruction require a lot of resources and time, and developers should use thread resources very economically. The best way is to use the thread pool. The thread pool can prevent the operating system from continuously switching threads due to a large number of threads currently in progress. When the number of threads reaches the upper limit, the thread will automatically wait in queue. When the thread resources are available, the thread tasks in the queue will be executed in sequence. If no waiting resources are waiting in queue, the thread will become idle.

Use ThreadPool to access the thread pool

This approach allows us to implement creation and reuse the thread logic without so much complexity, but it also has some limitations, such as its built-in method, we do not know when the task in the thread pool will end or obtain the return value of the thread. To solve these problems, Microsoft has introduced a new concept.

Use tasks to access the thread pool

After a Task is introduced, you can use the following implementation to replace ThreadPool.

These implementations are equivalent. The Task itself implements a lot of things that ThreadPool cannot do.

Use a Task to obtain the return value of a thread

 

Use Task to wait for the thread to end

For more information on the use of Task synchronization programming, see (not yet written). First, dig yourself into a pitfall O ).

Asynchronous Delegation

ThreadPool. QueueUserWorkItem does not provide a simple mechanism to obtain the return value of a thread. Asynchronous delegation solves this problem and supports the input of a series of parameters. In addition, exceptions not processed in asynchronous delegation can be easily re-thrown in the call thread (when EndInvoke is called), so no display processing is required.

Asynchronous delegation is used to execute tasks in the following steps:

Calling BeginInvoke does not block the current thread. Therefore, you can execute other operations you want to synchronize after the call.

Implement Asynchronous delegation in blocking mode

 

EndInvoke mainly does three tasks: 1. Waiting for the completion of asynchronous delegation 2. Receiving the return value 3. Throwing an unhandled exception in the asynchronous thread again in the current thread.

Non-blocking asynchronous Delegation

You can also specify a callback method when calling BeginInvoke. This method is automatically called when the asynchronous delegate ends. In this way, asynchronous delegation is automatically executed like a background thread without waiting by the main thread. You only need to perform some additional operations during BeginInvoke.

About Thread Pool

Jeffery provides some suggestions on the use of thread pools in C # via CLR Chapter27. Currently, developers are allowed to specify the maximum number of threads in a thread pool. However, it turns out that we should not specify the thread ceiling for a thread pool. Otherwise, the program may be deadlocked or starved to death. For example, you may have set 1000 threads, but at some point there are exactly 1,001st threads that need to wait for all threads to end before execution. In this case, if you limit the number of threads in the thread pool, A deadlock occurs. From another perspective of development, you should not limit the amount of resources a process uses, such as the amount of memory a process can use and the amount of bandwidth it uses. therefore, although you can use GetMaxThreads, SetMaxThreads, GetMinThreads, SetMinThreads, and GetAvailableThreads to limit the number of threads, he still does not recommend this. These restrictions may make your program run slower.

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.