在单一时间限制执行一个方法的线程数线程、时间、方法

由网友(花落未央)分享简介:我们遇到这样的情况,我们要限制paralell的请求数我们的应用程序可以为它的应用服务器。我们有运行潜在100+后台线程将要在某个时候发出呼叫到应用服务器,但只要5线程能够调用的SendMessage()(或任何方法将要)在任何一个时间。什么是实现这一目标的最佳方法是什么?We have a situation wh...

我们遇到这样的情况,我们要限制paralell的请求数我们的应用程序可以为它的应用服务器。我们有运行潜在100+后台线程将要在某个时候发出呼叫到应用服务器,但只要5线程能够调用的SendMessage()(或任何方法将要)在任何一个时间。什么是实现这一目标的最佳方法是什么?

We have a situation where we want to limit the number of paralell requests our application can make to its application server. We have potentially 100+ background threads running that will want to at some point make a call to the application server but only want 5 threads to be able to call SendMessage() (or whatever the method will be) at any one time. What is the best way of achieving this?

我使用看门者对象的某种阻止线程进入该方法,直到在执行它的线程数下降到低于阈值已经考虑。这将是一个合理的解决方案,还是我忽略了一个事实,这可能是脏/危险?

I have considered using some sort of gatekeeper object that blocks threads coming into the method until the number of threads executing in it has dropped below the threshold. Would this be a reasonable solution or am I overlooking the fact that this might be dirty/dangerous?

我们正在开发的C#.NET 3.5。

We are developing in C#.NET 3.5.

谢谢

史蒂夫

推荐答案

使用信号量

http://msdn.microsoft.com/en-us/library/system.threading.semaphore.aspx

限制的线程的数量可以   访问资源的资源或池   同时进行的。

Limits the number of threads that can access a resource or pool of resources concurrently.

阅读全文

相关推荐

最新文章