C # explicitly creates threads or uses thread pool threads--new thread () or ThreadPool.QueueUserWorkItem ()

Source: Internet
Author: User

In C # multithreaded programming, about whether to use the thread that you created (threads) or thread pool (ThreadPool) threads, has been very confused, know the Jeffrey Richter the relevant introduction to understand, recorded as follows:

When any of the conditions are met, you can explicitly create your own thread:

1. Threads need to run at a non-trivial thread priority level. All thread pool threads are running at normal priority, although it is not recommended to have the thread pool threads more prioritized. In addition, changes to the priority level are unsustainable between different thread pool operations.

2. A thread is required to behave as a foreground thread to prevent the application from terminating the end of the task. Thread pool threads are always background threads, and if the CLR terminates the process, they may not be able to accomplish the task.

3. The task of calculating the limit needs to run for a long time. Thread pool The logic used to determine whether an additional thread needs to be created is more complex. You can avoid this problem by creating a dedicated thread directly for long-running tasks.

4. To start the thread and possibly call the thread's Abort method to terminate it prematurely.

C # explicitly creates threads or uses thread pool threads--new thread () or ThreadPool.QueueUserWorkItem ()

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.