我是否需要担心阻止任务? [英] Do I need to worry about blocking tasks?

查看:152
本文介绍了我是否需要担心阻止任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要担心在.NET中阻止任务多少钱?即.NET任务调度程序如何处理线程池中的线程阻塞和超额订阅?

How much do I need to worry about blocking tasks in .NET? i.e. how does the .NET task scheduler handle blocking of threads in the thread pool and oversubscription?

例如。如果我在任务中有一些IO,我应该总是使用 LongRunning 提示创建它吗?或者任务调度程序启发式更好地处理它?在C ++中有一个 Oversubscribe 提示,它可以很好地工作,但我没有在.NET中找到任何等价物。

E.g. if I have some IO in a task, should I always create it with the LongRunning hint? Or does the task scheduler heuristics handle it better? In C++ there is an Oversubscribe hint which works perfectly but I have not found any equivalent in .NET.

推荐答案

ThreadPool会检测其中一个线程是否阻塞,并提示它将另一个线程添加到池中。所以,如果你阻塞很多,那么性能很可能不会很糟糕,因为ThreadPool会试图让你的CPU内核保持忙碌。

The ThreadPool does detect when one of its threads blocks and it is a hint for it to add another thread to the pool. So, if you block a lot, the performance most likely won't be terrible, because ThreadPool will try to keep your CPU cores busy.

但是有很多被阻塞的线程可以是一个性能问题,因为它会增加内存消耗并导致更多的上下文切换。

But having many blocked threads can be a performance problem, because it increases memory consumption and can lead to more context switches.

此外,这种行为可能会导致IO性能下降。对于旋转磁盘(HDD),同时访问许多文件会导致大量搜索,这会严重影响性能。

Also, this behavior may lead to decreased performance of IO. With spinning disks (HDDs), accessing many files at the same time causes lots of seeking, which can affect the performance drastically.

这篇关于我是否需要担心阻止任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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