异步和异步方法澄清? [英] Async and asynchronous methods clarification?

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

问题描述

AFAIK - 当处理(和我读了很多关于它),异步方法(不可以异步委托的!)的存在是为了解决线程被阻塞的问题 I / O操作这样的:读取一个文件或下载的文件:

里氏说明它很清楚这里:

  • 任务< T> 是不相关的I / O阻塞问题。那简直是就像开一个线程(加额外的效率+功能的) - 但它还是引起了线程占用CPU量子等

这里是我的问题:

我读过(MSDN)说:

  

这是异步方法提供了一种简便的方法做可能   长时间运行工作的不阻塞调用者线程。的来电   异步方法可以恢复其工作,而不等待异步   方法来完成。

  • ,是不是就是创建一个任务< T> ContinueWith

  • 不的术语混乱? 异步方法是为 I / O 操作(其中有线程在等待,而我/ O操作是由与没有线程正在处理它)。 但是调用code(使用异步)为:异步方法是一个有点混乱。你不觉得吗?因为我假设有正在执行另一个线程...(这是我的第一个问题实际上)。

来自哪里的困惑?

由于<一个href="http://books.google.co.il/books?id=VENrFSQFco8C&pg=PA927&lpg=PA927&dq=%22The%20problem%20just%20described%20might%20be%20insoluble%20if%20every%20thread%20needed%20to%20be%20busy%22&source=bl&ots=3uYZli808V&sig=eyDe6pdWBUnyacvqRzeTHYEPFD8&hl=en&sa=X&ei=FpO8UYnTDITvsgbk44CAAQ&redir_esc=y#v=onepage&q=%22The%20problem%20just%20described%20might%20be%20insoluble%20if%20every%20thread%20needed%20to%20be%20busy%22&f=false"相对=nofollow> 阿尔巴哈利往往强调 什么异步方法是:

P.S。我读过一些问题,在这里SO关于这个话题,却发现其中没有谈到异步方法来这里是为了处理IO操作的误判

解决方案
  

任务&LT; T&GT; 是不相关的I / O阻塞问题。这只不过是就像开一个线程(加上额外的效率和功能) - 但它还是引起了线程占用CPU量子等

不一定。基本上有两种工作取值:一是执行同步一块code和完成时code完成执行。这种工作,因为它开始执行,直到它的(成功与否)完成的全部时间。

但是,还有另一种工作:一,当有事​​完成。这种工作是什么样的.Net 4.5和C#5.0的使用严重,并没有阻止(在至少不是直接)。您可以创建这样的工作用自己 TaskCompletionSource&LT; T&GT;

(还有一点就是阻塞的线程不消耗任何CPU,但是这不是真的与此有关。)

  

,是不是就是创建一个任务&LT; T&GT; ContinueWith

计谋牛逼颇为相似 t.ContinueWith(REST方法)

  

不的术语混淆?异步方法对I / O操作(其中有零线程在等待,同时I / O操作是由无线程处理它)。但是,调用code(使用异步)为:异步方法比较混乱。难道你不觉得吗?因为我假设有正在执行另一个线程。

我不明白的混乱。经典异步方法(如的BeginRead();这被称为异步编程模型或APM)是在开始运作的方式,它完成时被通知(尽管回调)。现代异步方法(如 ReadAsync();这被称为基于任务的异步模式或TAP)也开始了操作,它完成时被通知(的一种方式使用等待)。

在这两种情况下,有可能是该方法返回(在code之前的第一个计谋在TAP的情况下),然后执行一些code。

在这两种情况下,被通知有关结果不会阻止任何线程通常的方式(回调APM,等待的TAP)。

在这两种情况下,你可以使用阻塞等待,如果你想(立即调用 EndXxx()法APM,等待()的TAP)。

这两种情况下,可以用来在一个委托APM,任务执行同步code在后台线程(的BeginInvoke()。 Factory.StartNew()的TAP)。

此外,我看不到的混乱,这两个模型看起来非常相似,我。

AFAIK - ( and I read a lot about it), asynchronous methods (not asynchronous delegates !) exists to solve the "thread is blocked" problem when dealing with I/O operations like : reading a file or downloading a file :

Richter shows it quite clearly here :

  • Task<T> is not related to the i/o blocking issue. it is simply just like open a thread ( plus extra efficiency + functionality ) - but it still causes a thread to consume cpu quanta etc.

And here is my question :

I've read (msdn) that :

An async method provides a convenient way to do potentially long-running work without blocking the caller's thread. The caller of an async method can resume its work without waiting for the async method to finish.

  • Is it just like creating a Task<t> with ContinueWith ?

  • Doesn't the terminology is confusing ? asynchronous methods are for i/o operations (where there are zero threads waiting while i/o operation is made and no thread is dealing with it). But to call a code (which use async) as: asynchronous methods is a bit confusing. don't you think ? because I assume there is another thread which is executing...(which is my first question actually).

Where is the confusion from ?

Because Albahari tend to emphasize what Asynchronous methods are for :

p.s. I've read a few question here at SO regarding this topic , but found none which talks about the misclassification that asynchronous methods are here to deal with io operations

解决方案

Task<T> is not related to the I/O blocking issue. It is simply just like open a thread (plus extra efficiency and functionality) - but it still causes a thread to consume CPU quanta etc.

Not necessarily. Basically there are two kinds of Tasks: one executes a synchronous piece of code and completes when that code finishes executing. This kind of Task blocks a Thread the whole time since it starts executing until it's completed (successfully or not).

But there is another kind of Task: one that completes when something happens. This kind of Task is what .Net 4.5 and C# 5.0 use heavily and it doesn't block a Thread (at least not directly). You can create such Task yourself by using TaskCompletionSource<T>.

(Another point is that a blocked thread doesn't consume any CPU, but that's not really relevant here.)

Is it just like creating a Task<t> with ContinueWith?

Yes, await t is quite similar to t.ContinueWith(rest of the method).

Doesn't the terminology is confusing? Asynchronous methods are for I/O operations (where there are zero threads waiting while I/O operation is made and no thread is dealing with it). But to call a code (which use async) as: asynchronous methods is a bit confusing. Don't you think? Because I assume there is another thread which is executing.

I don't see the confusion. Classic asynchronous method (like BeginRead(); this is called "Asynchronous Programming Model" or APM) is a way of starting an operation and being notified when it completes (though a callback). Modern async method (like ReadAsync(); this is called "Task-based Asynchronous Pattern" or TAP) is also a way of starting an operation and being notified when it completes (using await).

In both cases, there can be some code that executes before the method returns (the code before the first await in the TAP case).

In both cases, the usual way of being notified about the result doesn't block any threads (callback for APM, await for TAP).

In both cases, you can use blocking wait if you want (immediately calling the EndXxx() method for APM, Wait() for TAP).

Both cases can be used to execute synchronous code on a background thread (BeginInvoke() on a delegate for APM, Task.Factory.StartNew() for TAP).

Again, I don't see the confusion, the two models seem very similar to me.

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

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