Asynchronous method invocation explained

In multithreaded computer programming, asynchronous method invocation (AMI), also known as asynchronous method calls or the asynchronous pattern is a design pattern in which the call site is not blocked while waiting for the called code to finish. Instead, the calling thread is notified when the reply arrives. Polling for a reply is an undesired option.

Background

AMI is a design pattern for asynchronous invocation of potentially long-running methods of an object.[1] It is equivalent to the IOU ("I owe you") pattern described in 1996 by Allan Vermeulen.[2] [3]

In most programming languages a called method is executed synchronously, i.e. in the thread of execution from which it is invoked. If the method takes a long time to complete, 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 future objects.[4] 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 is a special case of procedure, asynchronous method invocation is a special case of asynchronous procedure call.

Implementations

Java class

FutureTask class[5] in Java use 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 software components.

.NET Framework

Example

The following example is loosely based on a standard AMI style used in the .NET Framework.[9] Given a method Accomplish, one adds two new methods BeginAccomplish and EndAccomplish:

class Example

Upon calling BeginAccomplish, the client immediately receives an object of type AsyncResult (which implements the IAsyncResult 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 EndAccomplish (passing the previously received object), which blocks until the method has completed and the result is available.[10] The AsyncResult object normally provides at least a method that allows the client to query whether the long-running method has already completed:

interface IAsyncResult

One can also pass a callback method to BeginAccomplish, to be invoked when the long-running method completes. It typically calls EndAccomplish 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.[11] [12]

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 AccomplishAsync instead of BeginAccomplish.[13] [14] 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 AccomplishCompleted) in the same thread in which BeginAccomplish 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.[15]

References

  1. Web site: Asynchronous Method Invocation . 22 November 2008 . Distributed Programming with Ice . ZeroC, Inc. . dead . https://web.archive.org/web/20080105093534/http://www.zeroc.com/doc/Ice-3.2.1/manual/Async.34.2.html . 5 January 2008 .
  2. Vermeulen . Allan . June 1996 . An Asynchronous Design Pattern . . 22 November 2008 .
  3. Book: Nash, Trey . Accelerated C# 2008 . 2007 . Apress . 978-1-59059-873-3 . Threading in C# .
  4. Lavender . R. Greg . Douglas C. Schmidt . Active Object . 22 November 2008 . https://web.archive.org/web/20120722180050/http://www.cs.wustl.edu/%7Eschmidt/PDF/Act-Obj.pdf . 2012-07-22 . dead . Douglas C. Schmidt .
  5. Web site: Class FutureTask . http://webarchive.loc.gov/all/20130625215130/http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/FutureTask.html . dead . 2013-06-25 . Oracle . 2011 . 2015-06-29 .
  6. Web site: Asynchronous Programming Model. Microsoft. 2015. 2015-06-29.
  7. Web site: Event-based Asynchronous Pattern Overview. Microsoft. 2015. 2015-06-29.
  8. Web site: Task-based Asynchronous Pattern. Microsoft. 2015. 2015-06-29.
  9. Web site: Asynchronous Programming Design Patterns . 22 November 2008 . .NET Framework Developer's Guide . Microsoft Developer Network. https://web.archive.org/web/20081122091746/http://msdn.microsoft.com/en-us/library/ms228969.aspx. 22 November 2008 . live.
  10. Web site: Asynchronous Programming Overview . 22 November 2008 . .NET Framework Developer's Guide . Microsoft Developer Network. https://web.archive.org/web/20081207092841/http://msdn.microsoft.com/en-us/library/ms228963.aspx. 7 December 2008 . live.
  11. Web site: Using an AsyncCallback Delegate to End an Asynchronous Operation . 22 November 2008 . .NET Framework Developer's Guide . Microsoft Developer Network. https://web.archive.org/web/20081223205326/http://msdn.microsoft.com/en-us/library/ms228972.aspx. 23 December 2008 . live.
  12. Web site: Concurrency Issues . 22 November 2008 . Distributed Programming with Ice . ZeroC, Inc. . dead . https://web.archive.org/web/20080328070322/http://www.zeroc.com/doc/Ice-3.2.1/manual/Async.34.3.html . 28 March 2008 .
  13. Book: Professional C# 2008. limited. Christian Nagel . Bill Evjen . Jay Glynn . Karli Watson . Morgan Skinner . amp . 570 - 571. Wiley. 2008. 9780470191378. Event-based Asynchronous Pattern.
  14. Web site: Multithreaded Programming with the Event-based Asynchronous Pattern . 22 November 2008 . .NET Framework Developer's Guide . Microsoft Developer Network. https://web.archive.org/web/20081225175311/http://msdn.microsoft.com/en-us/library/hkasytyf.aspx. 25 December 2008 . live.
  15. Web site: Deciding When to Implement the Event-based Asynchronous Pattern . 22 November 2008 . .NET Framework Developer's Guide . Microsoft Developer Network. https://web.archive.org/web/20081122092048/http://msdn.microsoft.com/en-us/library/ms228966.aspx. 22 November 2008 . live.

Further reading