异步方式和异步委托 [英] Asynchronous methods and asynchronous delegates

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

问题描述

简而言之C#3.0 说的异步方法异步委托的外观相似,但该行为是非常不同的。

下面是书上说的关于这两个。

异步方法

  1. 在很少或从来没有阻止任何线程。
  2. 开始的方法可能不会立即返回给调用者。
  3. 在一个约定的协议,没有C#语言的支持。

异步委托

  1. 可能会阻塞任何时间长度
  2. 的BeginInvoke 的立即返回给调用者。
  3. 内置的编译器的支持。

这本书还指出,异步方法的目的是为了让很多任务对几个线程运行;的异步委托的目的是要以平行与呼叫者执行任务

当我进去看了的的BeginRead()的方法的的System.IO.Stream 的通过反射类,它使用的是委托和调用的的BeginInvoke 这一点。这样的异步方法在内部使用异步委托。

  1. 在这种情况下,人们怎么能说他们的行为有什么不同?由于它采用代表国内,怎么像上面的比较是可能的吗?
  2. 请您觉得用委托的方式的BeginXXX工作是并行执行的函数调用者?
  3. 的方式
  4. 什么是喜欢保持充分利用CPU的所有优点,以实现异步方法的正确方法?

有什么想法?

解决方案

目前的核心,主要有两方面的行为,你可能会看到,当你调用BeginFoo()有一个回调。

  1. 工作开始在后台线程,以及该线程将要使用的整个时间,直到工作完成,并且回调调用(例如,因为工作是同步的)。
  2. 虽然在后台线程一些工作情况,线程不需要使用的整个时间(例如因为工作涉及系统IO,可安排在如IOCompletionPort回调)。

当您使用委托,行为#1以上的情况发生。

某些API(即对非阻塞IO的调用底层的支持)的支持行为,#2。

在流的具体情况,我不知道,但我的猜测是,它是一个抽象基类,所以这仅仅是为了实现对只读同步版本的子类的默认行为。 好的子类将重写BeginRead的/ EndRead有无阻塞的实现。

#2的优势,如你所说,是你可以拥有如而无需耗费100个线程(线程是昂贵的)100未决的IO调用。

C# 3.0 in a nutshell says asynchronous methods and asynchronous delegates looks similar but the behavior is very different.

Here is what the book says about both.

Asynchronous methods

  1. Rarely or never blocks any thread.
  2. Begin method may not immediately return to the caller.
  3. An agreed protocol with no C# language support.

Asynchronous delegates

  1. May block for any length of time
  2. BeginInvoke return immediately to the caller.
  3. Built-in compiler support.

The book also says, The purpose of asynchronous methods is to allow many tasks to run on few threads; the purpose of asynchronous delegates is to execute a task in parallel with the caller.

When I looked into the BeginRead() method in System.IO.Stream class through reflector, it is using a delegate and calling BeginInvoke on that. So an asynchronous method is using an asynchronous delegate internally.

  1. In such case, how can one say their behaviors are different? Since it uses delegates internally, how a comparison like the above is possible?
  2. Do you think working with a delegate's BeginXXX method is the way to execute a function in parallel to the caller?
  3. What is the proper way to implement asynchronous methods by maintaining all the advantages like making good use of CPU?

Any thoughts?

解决方案

At the core, there are two main behaviors you may see when you call BeginFoo() with a callback.

  1. Work is started on a background thread, and that thread will be used the entire time up until the work is completed and the callback is invoked (e.g. because the work is synchronous).
  2. Though some work happens on a background thread, the thread need not be in use the entire time (e.g. because the work involves System IO which can schedule callbacks on e.g. the IOCompletionPort).

When you use a delegate, behavior #1 above happens.

Some APIs (that have underlying support for non-blocking IO calls) support behavior #2.

In the specific case of 'Stream', I am not sure, but my guess is it is an abstract base class and so this is merely the default behavior for a subclass that implements only a synchronous version of Read. A 'good' subclass would override BeginRead/EndRead to have a non-blocking implementation.

The advantage of #2, as you said, is that you can have e.g. 100 pending IO calls without consuming 100 threads (threads are expensive).

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

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