使用TPL限制任务数量 [英] Limit number of tasks with TPL

查看:70
本文介绍了使用TPL限制任务数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究创建需要在多个线程上执行的任务.我可以创建大量任务,例如2000.我想限制同时排队和执行的任务数量.有没有办法创建一定数量的任务,然后在完成时创建新任务?如果任务计划程序对此有所帮助,请尝试进行计算.

I'm looking into creating tasks which need to be executed on multiple threads. I could have a large number of tasks being created i.e. 2000 for example. I want to limit the number of tasks queued and executed simultaneously. Is there a way to create a certain number of tasks and then create new ones as they complete? Trying to work out if the task scheduler helps with this.

用另一种方式来表达这句话...是有原因的,我应该限制同时创建/排队/执行的任务的数量,因为我可以有很多这样的任务.2000.任务计划程序是否以最佳方式计划了任务,似乎找不到任何有关其实际工作方式的信息...

To phrase this a different way...is there a reason I should want to limit the number of tasks created/queued/executed simultaneously given that I could have a really large number e.g. 2000. Does the task scheduler optimally schedule tasks, can't seem to find any info on how it actually works...

我没有使用Parallel.Foreach.我决定使用计数器,并根据计数器的最大数量以及当前的任务数量,创建任务或等待直到不超过最大数量.

I'm not using Parallel.Foreach. I've decided to use counters and based on a max number of counters, and the current number of tasks, create tasks or wait until the max number is not exceeded.

推荐答案

如果您要发送2000封电子邮件,则不想阻止当前线程,并且只想使用有限数量的线程,那么我认为您应该使用一个调用 Parallel.ForEach() Task .像这样:

If you want to send 2000 emails, you don't want to block the current thread and you want to use only a limited number of threads, then I think you should use a single Task that calls Parallel.ForEach(). Something like:

List<Email> emails = …;

var task = Task.Factory.StartNew(() =>
{
    Parallel.ForEach(
        emails,
        new ParallelOptions { MaxDegreeOfParallelism = maxNumberOfThreads },
        email => email.Send());
});

// handle the completion of task here

如果您不知道最佳线程数,则必须通过实验找到它.TPL能够很好地猜测出CPU绑定计算的最佳线程数.但是您的代码不是受CPU限制的,而是受网络限制的,这意味着线程的最佳数量与CPU拥有的内核数量无关,它取决于网络连接的带宽和延迟(可能还取决于网络连接的带宽和延迟).您的邮件服务器).

If you don't know the best number of threads, then you will have to find it out experimentally. TPL is able to guess the best number of threads for CPU-bound computations reasonably well. But your code is not CPU-bound, it's network-bound, which means the optimal number of threads has nothing to do with the number of cores your CPU has, it depends on the bandwidth and latency of your network connection (and possibly also of your mail server).

这篇关于使用TPL限制任务数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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