0

I have a method like this:

private Task GetLiveData(List<string> keys, ConcurrentBag<LiveData> resultsBag, CancellationToken cancellationToken)
{
     var tasks = new List<Task>();

     foreach (var key in keys)
     {
          cancellationToken.ThrowIfCancellationRequested();
          tasks.Add(ExecuteOne(assetId, semafore, resultsBag, cancellationToken));
     }

     return Task.WhenAll(tasks);
}

ExecuteOne method is execute asynchronous I/O operation and also convert data from redis to the model, but main part is async I/O operation (read from Redis)

If for example count of keys will be 10_000, then we will call Redis 10_000 times and add this to the list of tasks,

and my question is: Can we run out of available threads in threapool during this process (10_000 keys it's just example there could be more) so we will have threadpool starvation? If so, so what will be then, will it go to the queue and wait until thread will be available or will add a new thread to threadpool?

I'm not so acknowledged in this topic, so maybe my opinion is wrong

Thank you in advance

SValley_Dev
  • 638
  • 4
  • 8
  • Here are two relevant questions: [Why ThreadPool.QueueUserWorkItem is slow after thread count reach the limit of MinThreads?](https://stackoverflow.com/questions/67260454/why-threadpool-queueuserworkitem-is-slow-after-thread-count-reach-the-limit-of-m) / [ThreadPool SetMinThreads - the impact of setting it](https://stackoverflow.com/questions/46834697/threadpool-setminthreads-the-impact-of-setting-it) – Theodor Zoulias May 25 '22 at 11:29
  • 1
    As a side note, a possible effect of calling `cancellationToken.ThrowIfCancellationRequested();` inside the loop is to cause an exception while some tasks are already in-flight. These tasks will become fire-and-forget tasks, which is rarely what you want. The correct location for this call is at the top of the method, outside of the loop, before *any* task has been created. – Theodor Zoulias May 25 '22 at 11:33

0 Answers0