Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Asynchronous method invocation

From Wikipedia, the free encyclopedia
Software design pattern

Inmultithreadedcomputer programming,asynchronous method invocation (AMI), also known asasynchronous method calls or theasynchronous pattern is adesign pattern in which the call site is notblocked 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

[edit]

AMI is adesign pattern forasynchronous invocation of potentially long-runningmethods of anobject.[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 thethread 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 theactive object design pattern. Alternatives are synchronous method invocation andfuture 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.

Sincemethod is a special case ofprocedure,asynchronous method invocation is a special case ofasynchronous procedure call.

Implementations

[edit]

Java class

[edit]

FutureTask class[5] inJava useevents to solve the same problem. This pattern is a variant of AMI whose implementation carries more overhead, but it is useful for objects representingsoftware components.

.NET Framework

[edit]
  • Asynchronous Programming Model (APM) pattern (used before .NET Framework 2.0)[6]
  • Event-based Asynchronous Pattern (EAP) (used in .NET Framework 2.0)[7]
  • Task-based Asynchronous Pattern (TAP) (used in .NET Framework 4.0)[8]

Example

[edit]

The following example is loosely based on a standard AMI style used in the.NET Framework.[9]Given a methodAccomplish, one adds two new methodsBeginAccomplish andEndAccomplish:

classExample{ResultAccomplish(args)IAsyncResultBeginAccomplish(args)ResultEndAccomplish(IAsyncResulta)}

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

interfaceIAsyncResult{boolHasCompleted()}

One can also pass a callback method toBeginAccomplish, to be invoked when the long-running method completes. It typically callsEndAccomplish 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 namedAccomplishAsync instead ofBeginAccomplish.[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 typeAccomplishCompleted) in the same thread in whichBeginAccomplish 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

[edit]
  1. ^"Asynchronous Method Invocation".Distributed Programming with Ice. ZeroC, Inc. Archived fromthe original on 5 January 2008. Retrieved22 November 2008.
  2. ^Vermeulen, Allan (June 1996)."An Asynchronous Design Pattern".Dr. Dobb's Journal. Retrieved22 November 2008.
  3. ^Nash, Trey (2007). "Threading in C#".Accelerated C# 2008. Apress.ISBN 978-1-59059-873-3.
  4. ^Lavender, R. Greg;Douglas C. Schmidt."Active Object"(PDF). Archived fromthe original(PDF) on 2012-07-22. Retrieved22 November 2008.{{cite journal}}:Cite journal requires|journal= (help)
  5. ^"Class FutureTask". Oracle. 2011. Archived fromthe original on 2013-06-25. Retrieved2015-06-29.
  6. ^"Asynchronous Programming Model". Microsoft. 2015. Retrieved2015-06-29.
  7. ^"Event-based Asynchronous Pattern Overview". Microsoft. 2015. Retrieved2015-06-29.
  8. ^"Task-based Asynchronous Pattern". Microsoft. 2015. Retrieved2015-06-29.
  9. ^"Asynchronous Programming Design Patterns"..NET Framework Developer's Guide. Microsoft Developer Network.Archived from the original on 22 November 2008. Retrieved22 November 2008.
  10. ^"Asynchronous Programming Overview"..NET Framework Developer's Guide. Microsoft Developer Network.Archived from the original on 7 December 2008. Retrieved22 November 2008.
  11. ^"Using an AsyncCallback Delegate to End an Asynchronous Operation"..NET Framework Developer's Guide. Microsoft Developer Network.Archived from the original on 23 December 2008. Retrieved22 November 2008.
  12. ^"Concurrency Issues".Distributed Programming with Ice. ZeroC, Inc. Archived fromthe original on 28 March 2008. Retrieved22 November 2008.
  13. ^Christian Nagel; Bill Evjen; Jay Glynn; Karli Watson & Morgan Skinner (2008). "Event-based Asynchronous Pattern".Professional C# 2008. Wiley. pp. 570–571.ISBN 9780470191378.
  14. ^"Multithreaded Programming with the Event-based Asynchronous Pattern"..NET Framework Developer's Guide. Microsoft Developer Network.Archived from the original on 25 December 2008. Retrieved22 November 2008.
  15. ^"Deciding When to Implement the Event-based Asynchronous Pattern"..NET Framework Developer's Guide. Microsoft Developer Network.Archived from the original on 22 November 2008. Retrieved22 November 2008.

Further reading

[edit]
Gang of Four
patterns
Creational
Structural
Behavioral
Concurrency
patterns
Architectural
patterns
Other
patterns
Books
People
Communities
See also
Retrieved from "https://en.wikipedia.org/w/index.php?title=Asynchronous_method_invocation&oldid=1293140899"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp