方法异步意味着什么? [英] What does it mean for a method to be asynchronous?

查看:128
本文介绍了方法异步意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是异步方法.我想我知道,但我一直将其与并行性混淆.我不确定异步方法和并行方法之间有什么区别.

使用线程类和异步类还有什么区别?

编辑

一些说明异步,线程和并行性之间差异的代码将很有用.

解决方案

什么是异步方法?

当我们讨论潜在的冗长的操作时,将讨论异步方法.通常,我们需要完成这样的操作才能有意义地继续执行程序,但是我们不希望在操作完成之前暂停"(因为暂停可能意味着例如UI停止响应,这显然是不希望的).

异步方法是一种我们称之为冗长的操作 start 的方法.该方法应执行开始操作并非常迅速"返回所需的操作,以确保没有处理延迟.

异步方法通常返回一个令牌,调用者可以使用该令牌来查询操作是否已完成以及其结果是什么.在某些情况下,它们将回调(委托)作为参数.操作完成后,将调用回调以通知调用者其结果已准备好并将其传递回. 是常用的回调签名,尽管当然,通常回调看起来像任何东西.

那么谁真正执行了冗长的操作?

我在上面说过,异步方法会开始一个长度操作,但是在这种情况下开始"是什么意思?由于该方法会立即返回,因此实际的工作在哪里进行?

通常情况下,执行线程需要监视过程.因为不是线程调用了async方法,所以暂停了,谁呢?答案是,为此目的从托管线程池中选择了一个线程.

与线程有什么联系?

在这种情况下,我对线程"的解释很简单,就是您显式地旋转自己的线程并委托它来同步执行问题任务.该线程将阻塞一段时间,并在操作完成后向您的主"线程(可以继续执行)发出信号.

此指定的工作线程可能被从线程池中拉出(请注意:不建议在线程池线程中进行非常长的处理!),否则可能是您开始就是为了这个目的.

What is an asynchronous method. I think I know, but I keep confusing it with parallelism. I'm not sure what the difference between an asynchronous method is and what parallelism is.

Also what is difference between using threading classes and asynchronous classes?

EDIT

Some code demonstrating the difference between async, threading and parallelism would be useful.

解决方案

What are asynchronous methods?

Asynchronous methods come into the discussion when we are talking about potentially lengthy operations. Typically we need such an operation to complete in order to meaningfully continue program execution, but we don't want to "pause" until the operation completes (because pausing might mean e.g. that the UI stops responding, which is clearly undesirable).

An asynchronous method is one that we call to start the lengthy operation. The method should do what it needs to start the operation and return "very quickly" so that there are no processing delays.

Async methods typically return a token that the caller can use to query if the operation has completed yet and what its result was. In some cases they take a callback (delegate) as an argument; when the operation is complete the callback is invoked to signal the caller that their results are ready and pass them back. This is a commonly used callback signature, although of course in general the callback can look like anything.

So who does actually run the lengthy operation?

I said above that an async method starts a length operation, but what does "start" mean in this context? Since the method returns immediately, where is the actual work being done?

In the general case an execution thread needs to keep watch over the process. Since it's not the thread that called the async method that pauses, who does? The answer is, a thread picked for this purpose from the managed thread pool.

What's the connection with threading?

In this context my interpretation of "threading" is simply that you explicitly spin up a thread of your own and delegate it to execute the task in question synchronously. This thread will block for a time and presumably will signal your "main" thread (which is free to continue executing) when the operation is complete.

This designated worker thread might be pulled out of the thread pool (beware: doing very lengthy processing in a thread pool thread is not recommended!) or it might be one that you started just for this purpose.

这篇关于方法异步意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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