Why *not* change the priority of a ThreadPool (or Task) thread?

前端 未结 6 1415
情深已故
情深已故 2020-12-06 09:25

There are many places across the web and Stack Overflow where one is discouraged from changing the priority of a ThreadPool thread or TPL Task

6条回答
  •  [愿得一人]
    2020-12-06 09:53

    It's discouraged, specially if you're upping the priority, because it can affect the overall performance of your system.

    Not to offer an overcomplicated answer, but in general, thread priority is a complex topic. For example, Windows has 2 related descriptors: thread priority and process priority. Both range from Idle, the lowest, to Time critical, the highest. When you start a new process, it's set to the default, the mid-range (a normal process priority with a normal thread priority).

    Plus, thread priorities are relative, meaning that even setting a thread's priority to the highest in a busy system won't ensure that it will run in 'real-time'. DotNET offers no guarantees on this, neither does Windows. From that you can see why it's better to leave the threadpool alone, since, 99.9% of the time, it knows best :)

    All that said, it's ok to lower a thread's priority if the task involves a long computation. This won't affect other processes.

    Increasing priority, however, should only be done for tasks that need to react quickly, and have a short execution time, because this can negatively affect other processes.

提交回复
热议问题