A brief analysis of C # thread priority

Source: Internet
Author: User

C # Thread prioritization is necessary: if there are multiple threads running in the application, but some threads are more important than others, what to do in this case, you can assign different priorities to different threads in one process. In general, if a higher-priority thread is working, no time slices are allocated for the lower priority thread, and the advantage is that the thread that receives the user's input is given a higher priority. For most of the time, the thread does nothing, while the other threads perform their tasks. However, if the user enters information, the thread immediately gets a higher priority than the other threads in the application, processing user input events in a short period of time.

C # Thread Priority rule: high-priority threads can completely prevent low-priority threads from executing, so be careful when changing the priority of a thread. The priority of a thread can be defined as the value of the ThreadPriority enumeration, namely highest, AboveNormal, Normal, BelowNormal, and Lowest.

Note that each process has a basic priority, and these values are related to the priority of the process. Assigning a higher priority to a thread ensures that it takes precedence over other threads in the process, but other processes may be running on the system, and their threads have higher precedence. Therefore, Windows assigns high priority to its own operating system threads.

In the Threadplayaround example, the main () method is modified as follows to see the effect of modifying the thread's precedence:

 

   New ThreadStart (Startmethod);   New Thread (Workerstart);   " Worker " ;    = Threadpriority.abovenormal;  Workerthread.start ();  

Where the worker thread has a higher priority than the main thread, the result is as follows:

threadplayaroundwithpriorities
Interval to display results at> 1000000
starting Thread:main thread
Main thread:current Culture = en -US
starting Thread:worker
worker:current Culture = en -US
Main Thread:count has reached 1000000
Worker:count has reached 1000000
Worker:count has reached 2000000
Worker:count has reached 3000000
Worker:count has reached 4000000
Worker:count has reached 5000000
Worker:count has reached 6000000
Worker:count has reached 7000000
Worker:count has reached 8000000
Worker Thread finished
Main Thread:count has reached 2000000
Main Thread:count has reached 3000000
Main Thread:count has reached 4000000
Main Thread:count has reached 5000000
Main Thread:count has reached 6000000
Main Thread:count has reached 7000000
Main Thread:count has reached 8000000
Main Thread finished

This means that when the worker thread has a priority of AboveNormal, the main thread will no longer run once the worker thread is started.

The content of the C # thread priority is introduced here, and hopefully it will help you understand and learn about C # thread prioritization.

Original address: http://www.educity.cn/develop/1408740.html

A brief analysis of C # thread priority

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.