shubhamr
shubhamr

Reputation: 465

Limiting threads per Class

I have a .NET web application, which for each request sends requests to one of the api from a set of multiple API's (which api to hit depends on the request type), gets the response, processes it and returns the response.

A brief overview of the architecture

Suppose at any given point of time, lets say we have a maximum of 100 threads, and we get 100 requests (lets assume each thread is handling one request each) for which the request needs to go to API-1 and suddenly API-1's response time increases, and then we get subsequent requests which need to go to API-2,3 ... n and all these API(s) are working perfectly.Requests for these API(s) won't be served until one the threads get free from processing API-1 which results in an overall performance impact of the .NET web application.

What I want to achieve is that I want to limit the number of threads for each API (lets say we have a class for each api having some methods) such that each class does not exceed the maximum number of threads allocated to it.

(If I have n classes and 100 threads , I should be able to divide them into thread pools of 100/n each)

I tried a few links for thread pools but couldn't achieve the same I wanted.

Upvotes: 2

Views: 127

Answers (1)

Lionia Vasilev
Lionia Vasilev

Reputation: 12757

Probably your application is a good target for a asynchronous programming model which, if used correctly, eliminates the blocked threads downtime issue.

Asynchronous programming in C# is a very broad topic that has been discussed a lot. Check following resources:

If you really need to, you can still limit number of ThreadPool threads (workers) as usual. See Using TPL how do I set a max threadpool size discussion on StackOverflow.

Upvotes: 1

Related Questions