开发者

What is the best way to throttle many .NET Task instances?

开发者 https://www.devze.com 2023-02-10 23:42 出处:网络
I have created a large amount of Task instances.I need to run them all, and wait for them all to complete.The problem is that I need to make sure that no more than X tasks are between \"started\" and

I have created a large amount of Task instances. I need to run them all, and wait for them all to complete. The problem is that I need to make sure that no more than X tasks are between "started" and "completed" at any given time; the tasks involve calls to other parties that have a restriction on the number of simultaneous calls. Since these restrictions are not based on my hardware, I can't rely on any built in intelligent throttling; I need to enforce the limit strictly. I've been able to do this by having the tasks increment and decrement a shared variable in a thread-sa开发者_运维知识库fe way, but it seems unnecessarily cumbersome. Is there a way that's more built-in to the API directly, or an easy synchronization tool I'm missing?


You have to create your own task scheduler, see How to: Create a Task Scheduler That Limits the Degree of Concurrency

Edit: This requires more code than with Semaphore, but I have a feeling that this might perform better, because thread pool in which tasks are executed is unaware of your semaphore, but using TaskScheduler is playing by their rules.

Edit 2: One of possible drawback of using semaphore is that thread pool might think that your task is doing IO operations and schedule them a lot at a time (so that they hang there and wait, because they don't need CPU). Using TaskScheduler they will be scheduled exactly when there is place for them. It will definitely keep pool cleaner.


The Semaphore class might work for you: http://msdn.microsoft.com/en-us/library/system.threading.semaphore.aspx

There is a good example on this MSDN page to show you how to use it.

There may also be lighter-weight built-in thread pooling/synchronization mechanisms you can use to accomplish the same end, but Semaphore is basically designed to do what you are asking for.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号