多个等待vs Task.WaitAll-等效吗? [英] multiple awaits vs Task.WaitAll - equivalent?

查看:155
本文介绍了多个等待vs Task.WaitAll-等效吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就性能而言,这两种方法是否可以并行运行 GetAllWidgets() GetAllFoos()? / p>

是否有任何理由使用一个?似乎编译器在后台发生了很多事情,所以我不清楚。



=============方法A:使用多个等待====================

 公共异步任务< IHttpActionResult> MethodA()
{
var customer = new Customer();

customer.Widgets =等待_widgetService.GetAllWidgets();
customer.Foos =等待_fooService.GetAllFoos();

返回OK(客户);
}

============== MethodB :使用Task.WaitAll ===================

 公共异步Task< IHttpActionResult> MethodB()
{
var customer = new Customer();

var getAllWidgetsTask = _widgetService.GetAllWidgets();
var getAllFoosTask = _fooService.GetAllFos();

Task.WaitAll(new List [] {getAllWidgetsTask,getAllFoosTask});

customer.Widgets = getAllWidgetsTask.Result;
customer.Foos = getAllFoosTask.Result;

返回OK(客户);
}

=============== ====================

解决方案

第一个选项将不能同时执行两个操作。



第二个选项将同时执行,但将同步执行(即阻塞线程) )。



您不应同时使用这两个选项,因为第一个完成的速度比第二个慢,而第二个则不需要线程。



您应该使用 Task.WhenAll 异步等待这两项操作:

 公共异步任务< IHttpActionResult> MethodB()
{
var customer = new Customer();

var getAllWidgetsTask = _widgetService.GetAllWidgets();
var getAllFoosTask = _fooService.GetAllFos();

等待Task.WhenAll(getAllWidgetsTask,getAllFoosTask);

customer.Widgets =等待getAllWidgetsTask;
customer.Foos =等待getAllFoosTask;

返回OK(客户);
}

请注意,在 Task.WhenAll 完成了两个已经完成的任务,因此等待它们立即完成。


In terms of performance, will these 2 methods run GetAllWidgets() and GetAllFoos() in parallel?

Is there any reason to use one over the other? There seems to be a lot happening behind the scenes with the compiler so I don't find it clear.

============= MethodA: Using multiple awaits ======================

public async Task<IHttpActionResult> MethodA()
{
    var customer = new Customer();

    customer.Widgets = await _widgetService.GetAllWidgets();
    customer.Foos = await _fooService.GetAllFoos();

    return Ok(customer);
}

=============== MethodB: Using Task.WaitAll =====================

public async Task<IHttpActionResult> MethodB()
{
    var customer = new Customer();

    var getAllWidgetsTask = _widgetService.GetAllWidgets();
    var getAllFoosTask = _fooService.GetAllFos();

    Task.WaitAll(new List[] {getAllWidgetsTask, getAllFoosTask});

    customer.Widgets = getAllWidgetsTask.Result;
    customer.Foos = getAllFoosTask.Result;

    return Ok(customer);
}

=====================================

解决方案

The first option will not execute the two operations concurrently. It will execute the first and await its completion, and only then the second.

The second option will execute both concurrently but will wait for them synchronously (i.e. while blocking a thread).

You shouldn't use both options since the first completes slower than the second and the second blocks a thread without need.

You should wait for both operations asynchronously with Task.WhenAll:

public async Task<IHttpActionResult> MethodB()
{
    var customer = new Customer();

    var getAllWidgetsTask = _widgetService.GetAllWidgets();
    var getAllFoosTask = _fooService.GetAllFos();

    await Task.WhenAll(getAllWidgetsTask, getAllFoosTask);

    customer.Widgets = await getAllWidgetsTask;
    customer.Foos = await getAllFoosTask;

    return Ok(customer);
}

Note that after Task.WhenAll completed both tasks already completed so awaiting them completes immediately.

这篇关于多个等待vs Task.WaitAll-等效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆