gpt4 book ai didi

c# - 嵌套Task.WhenAll有什么限制吗?

转载 作者:行者123 更新时间:2023-12-05 03:25:21 25 4
gpt4 key购买 nike

让我们想象一些抽象代码

    private void Main()
{
var workTask1 = DoWork1();
var workTask2 = DoWork2();
var workTask3 = DoWork3();

await Task.WhenAll(workTask1, workTask2, workTask3);

AnalyzeWork(workTask1.Result, workTask2.Result, workTask3.Result);
}

private async Task<object> DoWork1()
{
var someOperationTask1 = someOperation1();
var someOperationTask2 = someOperation2();

await Task.WhenAll(someOperationTask1, someOperationTask2);

return new object
{
SomeOperationResult1 = someOperationTask1.Result,
SomeOperationResult2 = someOperationTask2.Result,
};
}

private async Task<object> DoWork2()
{
var someOperationTask3 = someOperation3();
var someOperationTask4 = someOperation4();

await Task.WhenAll(someOperationTask3, someOperationTask4);

return new object
{
SomeOperationResult3 = someOperationTask3.Result,
SomeOperationResult4 = someOperationTask4.Result,
};
}

private async Task<object> DoWork3()
{
var someOperationTask5 = someOperation5();
var someOperationTask6 = someOperation6();

await Task.WhenAll(someOperationTask5, someOperationTask6);

return new object
{
SomeOperationResult5 = someOperationTask5.Result,
SomeOperationResult6 = someOperationTask6.Result,
};
}

其中 3 个方法并行运行,每个方法都包含 2 个并行操作。并将 3 个方法的结果传递给某个方法。

我的问题是有什么限制吗?可以嵌套 Task.WhenAll 吗?嵌套 Task.WhenAll 和一级 Task.WhenAll 操作有什么区别?

最佳答案

唯一的限制是系统的可用内存。 Task.WhenAll 方法为每个未完成的任务附加一个延续,并在该任务完成时分离该延续。 continuation 是类似于 Task 的轻量级对象。它与调用 Task.ContinueWith 时得到的结果非常相似方法。每个延续的权重或多或少约为 100 个字节。它不太可能对您的程序产生任何显着影响,除非您需要同时Task.WhenAll 数千万个(或更多)任务。

如果你想直观地了解这个方法的内部结构,下面是其实现的粗略草图:

// For demonstration purposes only. This code is full of bugs.
static Task WhenAll(params Task[] tasks)
{
var tcs = new TaskCompletionSource();
int completedCount = 0;
foreach (var task in tasks)
{
task.ContinueWith(t =>
{
completedCount++;
if (completedCount == tasks.Length) tcs.SetResult();
});
}
return tcs.Task;
}

关于c# - 嵌套Task.WhenAll有什么限制吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72026350/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com