The Parallel.ForEachAsync
method invokes the asynchronous body
delegate on ThreadPool
threads. Usually this delegate returns a ValueTask
quickly, but in your case this is not what happens, because your delegate is not really asynchronous:
async (i, token) => Thread.Sleep(1000);
You are probably getting here a compiler warning, about an async
method lacking an await
operator. Nevertheless giving a mixed sync/async workload to the Parallel.ForEachAsync
method is OK. This method is designed to handle any kind of workload. But if the workload is mostly synchronous, the result might be a saturated ThreadPool
.
The ThreadPool
is said to be saturated when it has already created the number of threads specified by the SetMinThreads
method, which by default is equal to Environment.ProcessorCount
, and there is more demand for work to be done. In this case the ThreadPool
switches to a conservative algorithm that creates one new thread every second (as of .NET 6). This behavior is not documented precisely, and might change in future .NET versions.
In order to get the behavior that you want, which is to run the delegate for all 1000 inputs in parallel, you'll have to increase the number of threads that the ThreadPool
creates instantly on demand:
ThreadPool.SetMinThreads(1000, 1000); // At the start of the program
Some would say that after doing so you won't have a thread pool any more, since a thread pool is meant to be a small pool of reusable threads. But if you don't care about the semantics and just want to get the job done, whatever the consequences are regarding memory consumption and overhead at the operating system level, that's the easiest way to solve your problem.