Content deleted Content added
m Reverted edit by 83.65.213.82 (talk) to last version by GreenC bot |
|||
(27 intermediate revisions by 18 users not shown) | |||
Line 1:
{{Short description|Software design pattern}}
In
== Background ==
'''AMI''' is a [[software design pattern|design pattern]] for [[
It is equivalent to the
In most programming languages a called method is executed synchronously, i.e. in the [[thread (computer science)|thread of execution]] from which it is invoked. If the method
One common use of AMI is in the [[active object]] design pattern. Alternatives are synchronous method invocation and [[futures and promises|future objects]].<ref name="active object">{{cite journal | last=Lavender | first=R. Greg | author2=
An example for an application that may make use of AMI is a web browser that needs to display a web page even before all images are loaded.
Since [[method (computer science)|method]] is a special case of [[Procedure_(computer_science)|procedure]], '''asynchronous method invocation''' is a special case of [[asynchronous procedure call]].
== Implementations ==
=== Java class ===
FutureTask class<ref>{{cite web|title
=== .NET Framework ===
Line 22 ⟶ 25:
====Example====
The following example is loosely based on a standard AMI style used in the [[.NET Framework]].<ref name="ms228969">{{cite web |url=http://msdn.microsoft.com/en-us/library/ms228969.aspx |title=Asynchronous Programming Design Patterns |
Given a method <code>Accomplish</code>, one adds two new methods <code>BeginAccomplish</code> and <code>EndAccomplish</code>:
<
Result Accomplish(args …)
IAsyncResult BeginAccomplish(args …)
Result EndAccomplish(IAsyncResult a)
…
}</syntaxhighlight>
Upon calling <code>BeginAccomplish</code>, the client immediately receives an object of type <code>AsyncResult</code> (which implements the <code>IAsyncResult</code> interface), so it can continue the calling thread with unrelated work. In the simplest case, eventually there is no more such work, and the client calls <code>EndAccomplish</code> (passing the previously received object), which blocks until the method has completed and the result is available.<ref name="ms228963">{{cite web |url=http://msdn.microsoft.com/en-us/library/ms228963.aspx |title=Asynchronous Programming Overview |
<
bool HasCompleted()
…
}</syntaxhighlight>
One can also pass a callback method to <code>BeginAccomplish</code>, to be invoked when the long-running method completes. It typically calls <code>EndAccomplish</code> to obtain the return value of the long-running method. A problem with the callback mechanism is that the callback function is naturally executed in the worker thread (rather than in the original calling thread), which may cause race conditions.<ref name="ms228972">{{cite web |url=http://msdn.microsoft.com/en-us/library/ms228972.aspx |title=Using an AsyncCallback Delegate to End an Asynchronous Operation |
In the .NET Framework documentation, the term event-based asynchronous pattern refers to an alternative API style (available since .NET 2.0) using a method named <code>AccomplishAsync</code> instead of <code>BeginAccomplish</code>.<ref name=nageletal>{{cite book|title=Professional C# 2008|
A superficial difference is that in this style the return value of the long-running method is passed directly to the callback method. Much more importantly, the API uses a special mechanism to run the callback method (which resides in an event object of type <code>AccomplishCompleted</code>) in the same thread in which <code>BeginAccomplish</code> was called. This eliminates the danger of race conditions, making the API easier to use and suitable for software components; on the other hand this implementation of the pattern comes with additional object creation and synchronization overhead.<ref name="ms228966">{{cite web |url=http://msdn.microsoft.com/en-us/library/ms228966.aspx |title=Deciding When to Implement the Event-based Asynchronous Pattern |
==References==
Line 52 ⟶ 55:
== Further reading ==
* {{cite book|title=Programming WPF|
* [http://articles.techrepublic.com.com/5100-10878_11-1044325.html Using asynchronous method calls in C#]
|