Content deleted Content added
Separated summary and background paragraphs |
Separated Java and .NET implementations |
||
Line 4:
'''AMI''' is a [[software design pattern|design pattern]] for [[asynchronous I/O|asynchronous]] invocation of potentially long-running [[method (computer science)|methods]] of an [[object (computer science)|object]].<ref name="Async.34.2#71139">{{cite web |url=http://www.zeroc.com/doc/Ice-3.2.1/manual/Async.34.2.html#71139 |title=Asynchronous Method Invocation |accessdate= 22 November 2008 |work=Distributed Programming with Ice |publisher=ZeroC, Inc.}}</ref>
It is equivalent to the [[IOU]] pattern described in 1996 by Allan Vermeulen.<ref>{{cite journal |last=Vermeulen |first=Allan |date=June 1996 |title=An Asynchronous Design Pattern |journal=[[Dr. Dobb's Journal]] |url=http://www.ddj.com/184409898 |accessdate=22 November 2008 }}</ref><ref>{{cite book |last=Nash |first=Trey |title=Accelerated C# 2008 | year=2007 |publisher=Apress |isbn=978-1-59059-873-3 |chapter=Threading in C# }}</ref>
The event-based asynchronous pattern<ref>{{cite web| title=Event-based Asynchronous Pattern Overview|url = https://msdn.microsoft.com/en-us/library/wewwczdw(v=vs.110).aspx|publisher=Microsoft|date=2015|access-date=2015-06-29}}</ref> in [[.NET Framework]] and the FutureTask class<ref>{{cite web|title = Class FutureTask| url = https://docs.oracle.com/javase/6/docs/api/java/util/concurrent/FutureTask.html(v=vs.110).aspx|publisher=Oracle|date=2011|access-date=2015-06-29}}</ref> in [[Java (programming language)|Java]] use [[event (synchronization primitive)|events]] to solve the same problem. This pattern is a variant of AMI whose implementation carries more overhead, but it is useful for objects representing [[Component-based software engineering|software components]].▼
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 needs a long time to completion, e.g. because it is loading data over the internet, the calling thread is blocked until the method has finished. When this is not desired, it is possible to start a "worker thread" and invoke the method from there. In most programming environments this requires many lines of code, especially if care is taken to avoid the overhead that may be caused by creating many threads. AMI solves this problem in that it augments a potentially long-running ("synchronous") object method with an "asynchronous" variant that returns immediately, along with additional methods that make it easy to receive notification of completion, or to wait for completion at a later time.
One common use of AMI is in the [[active object]] design pattern. Alternatives are synchronous method invocation and [[futures and promises|future objects]]
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.
== Implementations ==
==Example==▼
=== Java class ===
▲
=== .NET Framework ===
* Asynchronous Programming Model (APM) pattern (used before .NET Framework 2.0)<ref>{{cite web|title=Asynchronous Programming Model|url=https://msdn.microsoft.com/en-us/library/ms228963(v=vs.110).aspx|publisher=Microsoft|date=2015|access-date=2015-06-29}}</ref>
* Event-based Asynchronous Pattern (EAP) (used in .NET Framework 2.0)<ref>{{cite web| title=Event-based Asynchronous Pattern Overview|url = https://msdn.microsoft.com/en-us/library/wewwczdw(v=vs.110).aspx|publisher=Microsoft|date=2015|access-date=2015-06-29}}</ref>
* Task-based Asynchronous Pattern (TAP) (used in .NET Framework 4.0)<ref>{{cite web|title=Task-based Asynchronous Pattern|url=https://msdn.microsoft.com/en-us/library/hh873175(v=vs.110).aspx|publisher=Microsoft|date=2015|access-date=2015-06-29}}</ref>
▲====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 |accessdate=22 November 2008 |work=.NET Framework Developer's Guide |publisher=Microsoft Developer Network| archiveurl= http://web.archive.org/web/20081122091746/http://msdn.microsoft.com/en-us/library/ms228969.aspx| archivedate= 22 November 2008 <!--DASHBot-->| deadurl= no}}</ref>
|