为什么*不*改变一个线程池(或任务)线程的优先级? [英] Why *not* change the priority of a ThreadPool (or Task) thread?

查看:297
本文介绍了为什么*不*改变一个线程池(或任务)线程的优先级?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有在网络和许多地方的计算器的其中一个是从改变线程池螺纹或第三方物流任务的优先级气馁。特别是:

There are many places across the web and stackoverflow where one is discouraged from changing the priority of a ThreadPool thread or TPL Task. In particular:

"你有超过一个线程池线程的状态和优先级没有控制和QUOT;
"运行时管理的线程池。您可以在线程调度无法控制,也无法改变线程的优先级"。

"You have no control over the state and priority of a thread pool thread."
"The runtime manages the thread pool. You have no control over the scheduling of the thread, nor can you change the thread's priority."

<一个href="http://stackoverflow.com/questions/1467194/does-changing-the-culture-of-a-threadpool-thread-affect-it-when-it-gets-returned"> 你不应该改变文化或优或...一个PoolThread的。就像你不油漆或重新装修租车。

"You should not change the Culture or Priority or ... of a PoolThread. Just like you don't paint or re-decorate a rental car."

"There are several scenarios in which it is appropriate to create and manage your own threads instead of using thread pool threads: (such as...) you require a thread to have a particular priority."

<一个href="http://groups.google.com/group/microsoft.public.dotnet.general/browse_thread/thread/a19ccff5b99411c7/d7c6d0bedfa1f331"> 在线程池的每个线程运行在默认优先级和code要更改ThreadPriority没有任何影响。

"Each thread in ThreadPool runs at the default priority and the code to change the ThreadPriority has no effect."

不过,这是一个简单的事情,这样做,调试器显示的变化的确实的似乎要坚持(只要该值可以被读回)。

However, it is a simple matter to do so, and the debugger shows that the change does seem to stick (insofar as the value can be read back).

Thread.CurrentThread.Priority = ThreadPriority.AboveNormal;

所以,问题是,什么是具体的的原因作为这个特殊的禁忌?

So the question is, what is the specific reason for this particular taboo?

我的怀疑:这样做扰乱了池的微妙负载均衡-假设。但是这并不能解释为什么一些消息人士说,您不可以更改它。

My suspicion: doing so disturbs the delicate load balancing-assumptions of the pool. But this doesn't explain why some sources say that you can't change it.

推荐答案

线程池,尤其是.NET 4.0线程池,有很多花样了它的袖子,是一个相当复杂的系统。添加的任务和任务调度和工作窃取和其他各种东西,但现实是,你不知道是怎么回事。线程池可能会注意到你的任务等待I / O,并决定安排你的任务东西快速或中止你的线程来运行的东西更高的优先级。你的线程可能在某种程度上是依赖于一个高优先级的线程(你可能会或可能不知道的),并最终导致死锁。你的线程可能会死在一些不正常的方式无法恢复的优先级。

The thread pool, especially the .NET 4.0 thread pool, has many tricks up its sleeve and is a rather complicated system. Add in tasks and task schedulers and work stealing and all sorts of other things and the reality is you don't know what is going on. The thread pool may notice that your task is waiting on I/O and decide to schedule something quick on your task or suspend your thread to run something of higher priority. Your thread may somehow be a dependency for a higher-priority thread (that you may or may not be aware of) and end up causing a deadlock. Your thread may die in some abnormal way and be unable to restore priority.

如果你有一个长期运行的任务,这样你认为这将是最好的,你的线程优先级较低那么线程池可能不适合你。虽然算法进行了改进在.NET 4.0中,它仍然是最适合用于短命的任务,在那里创建一个新线程的成本是不成比例的任务的长度。如果您的任务运行超过一两秒钟创建一个新线程的开销是微不足道的(尽管管理层可能是讨厌)。

If you have a long-running task such that you think it would be best that your thread have a lower priority then the thread pool probably isn't for you. While the algorithms have been improved in .NET 4.0, it is still best used for short-lived tasks where the cost of creating a new thread is disproportional to the length of the task. If your task runs for more than a second or two the cost of creating a new thread is insignificant (although management might be annoying).

这篇关于为什么*不*改变一个线程池(或任务)线程的优先级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆