Java class FutureTask class 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 • Asynchronous Programming Model (APM) pattern (used before .NET Framework 2.0) • Event-based Asynchronous Pattern (EAP) (used in .NET Framework 2.0) • Task-based Asynchronous Pattern (TAP) (used in .NET Framework 4.0)
Example The following example is loosely based on a standard AMI style used in the
.NET Framework. Given a method Accomplish, one adds two new methods BeginAccomplish and EndAccomplish: class Example { Result Accomplish(args …) IAsyncResult BeginAccomplish(args …) Result EndAccomplish(IAsyncResult a) … } 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. 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 { bool HasCompleted() … } 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. 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. 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. ==References==