【发布时间】:2021-07-10 13:33:30
【问题描述】:
如果我有这个
static void Main(string[] args)
{
var tasks = new List<Task>();
for (int i = 0; i < 10; i++)
{
tasks.Add(AsyncWithBlockingIO(i));
}
Task.WaitAll(tasks.ToArray());
}
private static async Task AsyncWithBlockingIO(int fileNum)
{
var result = await File.ReadAllTextAsync($"File{fileNum}").ConfigureAwait(false);
//will the below run concurrently on multiple threads?
CpuNoIOWork(result);
}
CpuNoIOWork() 会在 IO 调用完成时同时在多个线程上运行(使用线程池)还是一次只使用 1 个线程?
【问题讨论】:
-
你可以在
CpuNoIOWork()后面加上Debug.WriteLine(Thread.CurrentThread.ManagedThreadId);来证明continuation确实都是并发运行的。 -
您可能想看看这个:Why File.ReadAllLinesAsync() blocks the UI thread? 并非所有的异步广告方法都是真正异步的。
-
@TheodorZoulias,谢谢!没有在实际代码中使用文件 API,但了解一下很有用。
标签: c# .net multithreading async-await