异步/计谋 - 这是理解是否正确? [英] async/await - Is this understanding correct?

查看:164
本文介绍了异步/计谋 - 这是理解是否正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在两个问题,许多混乱 - 我不知道如果我终于得到了它的权利。这是我的理解:

After two questions and much confusion - I wonder if I finally got it right. This is my understanding:

异步/计谋提供的的目的只有一个的 - 以允许执行code后的的异步任务完成。 例如,

async/await serves only one purpose - to allow code to be executed after an already asynchronous task is finished. e.g.

async Task CallerMethod()
{
     await AsyncMethod();
     AnotherMethod();
}

允许 AnotherMethod 异步后执行 AsyncMethod 立即完成的的,而不是在 AsyncMethod 启动的。

allows AnotherMethod to be executed after the asynchronous AsyncMethod is finished instead of immediately after AsyncMethod is started.

异步/计谋NEVER的使的任何异步的。它不会启动一个单独的线程(除非已久的方法做,当然是反正)等。

async/await NEVER makes anything asynchronous. It doesn't start a separate thread (unless the awaited method does that anyway, of course), etc.

我的理解是(最终)是否正确?

Is my understanding (finally) correct?

推荐答案

尽管斯蒂芬的答案是正确的,我要确保你已经获得了一些加分清楚。

Though Stephen's answer is correct, I want to make sure that you've got a few additional points clear.

异步/计谋从来不做任何异步

async/await NEVER makes anything asynchronous

这使得 CallerMethod 异步方法。 CallerMethod 返回工作这本身就可以等待,而 CallerMethod 返回 CallerMethod 本身是完成前的任务,所以它是一个异步方法。

It makes CallerMethod an asynchronous method. CallerMethod returns a Task which can itself be awaited, and CallerMethod returns that task before the work of CallerMethod is itself complete, so it is an asynchronous method.

这是没有 AsyncMethod 异步的;已经是异步的。

It doesn't make AsyncMethod asynchronous; it was already asynchronous.

这不启动一个单独的线程

It doesn't start a separate thread

右键。这是混乱的一个常见来源。线程是一个单元的并发性,这只是一种不同步。打个比方通常有助于。你可以把面包烤面包机,等待它得到烤,然后使卵子。你可以把面包烤面包机,煮鸡蛋,而面包的烘烤,然后再处理吐司鸡蛋在完成后。或者,你可以雇佣2名厨师,一个煮鸡蛋,一到做面包。第一个是同步处理。第二和第三个是异步的,但只有第三是并发的。注意,第三溶液是最昂贵的;线程是工人和工人并不便宜。

Right. This is a frequent source of confusion. Threads are a unit of concurrency, which is only one kind of asynchrony. An analogy usually helps. You can put the bread in the toaster, wait for it to get toasted, and then make the eggs. You can put the bread in the toaster, cook the eggs while the toast is toasting, and then deal with the toast after the eggs are done. Or you can hire two cooks, one to cook eggs and one to make toast. The first is synchronous processing. The second and third are asynchronous, but only the third is concurrent. Notice that the third solution is the most expensive; threads are workers and workers aren't cheap.

是什么让一个异步方法异步不在于它是并发的 - 虽然它可能是。是什么使得它异步的是,它为您提供了一种机制,可以让你的做其他的事情你等待它的工作完成,而的。 等待只是一个愉快的方式来写这里的code我希望你能运行后,任务已经圆满完成。

What makes an asynchronous method asynchronous is not that it is concurrent -- though it might be. What makes it asynchronous is that it gives you a mechanism that allows you to do something else while you're waiting for its work to complete. await is just a pleasant way to write "here's the code I want you to run after the task has completed successfully".

这篇关于异步/计谋 - 这是理解是否正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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