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

前端 未结 6 1453
情深已故
情深已故 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 10:10

    Lowering the priority could also lead to unexpected consequences.

    Imagine you schedule a task in an application, but also call into a library that schedules several other tasks. Say you lower the thread priority while the app task runs. You could then end up with the normal priority tasks in the lib waiting for that low priority task to finish, if the pool doesn't spawn many threads, but the low priority thread it may not be given much CPU time if the rest of the system has many normal priority threads that want to run.

    Increasing the number of pool threads would alleviate this, at the cost of wasting more memory on stacks and spending more CPU time on context switches.

提交回复
热议问题