Asynchronous method invocation: Difference between revisions

Content deleted Content added
Gpeja (talk | contribs)
Separated Java and .NET implementations
BG19bot (talk | contribs)
m WP:CHECKWIKI error fix for #61. Punctuation goes before References. Do general fixes if a problem exists. - using AWB (11268)
Line 7:
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]].<ref name="active object">{{cite journal | last=Lavender | first=R. Greg |author2=[[Douglas C. Schmidt]] | title=Active Object | url=http://www.cs.wustl.edu/~schmidt/PDF/Act-Obj.pdf | format=PDF | accessdate=22 November 2008 }}</ref>.
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 ==
 
=== Java class ===
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]].