<s>
In	O
multithreaded	B-Operating_System
computer	B-General_Concept
programming	I-General_Concept
,	O
asynchronous	B-Operating_System
method	I-Operating_System
invocation	I-Operating_System
(	O
AMI	O
)	O
,	O
also	O
known	O
as	O
asynchronous	B-Operating_System
method	B-Language
calls	I-Language
or	O
the	O
asynchronous	B-Operating_System
pattern	O
is	O
a	O
design	O
pattern	O
in	O
which	O
the	O
call	O
site	O
is	O
not	O
blocked	B-Operating_System
while	O
waiting	O
for	O
the	O
called	O
code	O
to	O
finish	O
.	O
</s>
<s>
Instead	O
,	O
the	O
calling	O
thread	B-Operating_System
is	O
notified	O
when	O
the	O
reply	O
arrives	O
.	O
</s>
<s>
AMI	O
is	O
a	O
design	O
pattern	O
for	O
asynchronous	B-Operating_System
invocation	O
of	O
potentially	O
long-running	O
methods	B-Language
of	O
an	O
object	O
.	O
</s>
<s>
In	O
most	O
programming	O
languages	O
a	O
called	O
method	B-Language
is	O
executed	O
synchronously	O
,	O
i.e.	O
</s>
<s>
in	O
the	O
thread	B-Operating_System
of	I-Operating_System
execution	I-Operating_System
from	O
which	O
it	O
is	O
invoked	O
.	O
</s>
<s>
If	O
the	O
method	B-Language
takes	O
a	O
long	O
time	O
to	O
complete	O
,	O
e.g.	O
</s>
<s>
because	O
it	O
is	O
loading	O
data	O
over	O
the	O
internet	O
,	O
the	O
calling	O
thread	B-Operating_System
is	O
blocked	B-Operating_System
until	O
the	O
method	B-Language
has	O
finished	O
.	O
</s>
<s>
When	O
this	O
is	O
not	O
desired	O
,	O
it	O
is	O
possible	O
to	O
start	O
a	O
"	O
worker	O
thread	B-Operating_System
"	O
and	O
invoke	O
the	O
method	B-Language
from	O
there	O
.	O
</s>
<s>
In	O
most	O
programming	O
environments	O
this	O
requires	O
many	O
lines	O
of	O
code	O
,	O
especially	O
if	O
care	O
is	O
taken	O
to	O
avoid	O
the	O
overhead	O
that	O
may	O
be	O
caused	O
by	O
creating	O
many	O
threads	B-Operating_System
.	O
</s>
<s>
AMI	O
solves	O
this	O
problem	O
in	O
that	O
it	O
augments	O
a	O
potentially	O
long-running	O
(	O
"	O
synchronous	O
"	O
)	O
object	O
method	B-Language
with	O
an	O
"	O
asynchronous	B-Operating_System
"	O
variant	O
that	O
returns	O
immediately	O
,	O
along	O
with	O
additional	O
methods	B-Language
that	O
make	O
it	O
easy	O
to	O
receive	O
notification	O
of	O
completion	O
,	O
or	O
to	O
wait	O
for	O
completion	O
at	O
a	O
later	O
time	O
.	O
</s>
<s>
One	O
common	O
use	O
of	O
AMI	O
is	O
in	O
the	O
active	B-Operating_System
object	I-Operating_System
design	O
pattern	O
.	O
</s>
<s>
Alternatives	O
are	O
synchronous	O
method	B-Language
invocation	O
and	O
future	B-Operating_System
objects	I-Operating_System
.	O
</s>
<s>
Since	O
method	B-Language
is	O
a	O
special	O
case	O
of	O
procedure	O
,	O
asynchronous	B-Operating_System
method	I-Operating_System
invocation	I-Operating_System
is	O
a	O
special	O
case	O
of	O
asynchronous	B-Device
procedure	I-Device
call	I-Device
.	O
</s>
<s>
FutureTask	O
class	O
in	O
Java	B-Language
use	O
events	B-Operating_System
to	O
solve	O
the	O
same	O
problem	O
.	O
</s>
<s>
This	O
pattern	O
is	O
a	O
variant	O
of	O
AMI	O
whose	O
implementation	O
carries	O
more	O
overhead	O
,	O
but	O
it	O
is	O
useful	O
for	O
objects	O
representing	O
software	B-Architecture
components	I-Architecture
.	O
</s>
<s>
The	O
following	O
example	O
is	O
loosely	O
based	O
on	O
a	O
standard	O
AMI	O
style	O
used	O
in	O
the	O
.NET	B-Application
Framework	I-Application
.	O
</s>
<s>
Given	O
a	O
method	B-Language
Accomplish	O
,	O
one	O
adds	O
two	O
new	O
methods	B-Language
BeginAccomplish	O
and	O
EndAccomplish	O
:	O
</s>
<s>
Upon	O
calling	O
BeginAccomplish	O
,	O
the	O
client	O
immediately	O
receives	O
an	O
object	O
of	O
type	O
AsyncResult	O
(	O
which	O
implements	O
the	O
IAsyncResult	O
interface	O
)	O
,	O
so	O
it	O
can	O
continue	O
the	O
calling	O
thread	B-Operating_System
with	O
unrelated	O
work	O
.	O
</s>
<s>
In	O
the	O
simplest	O
case	O
,	O
eventually	O
there	O
is	O
no	O
more	O
such	O
work	O
,	O
and	O
the	O
client	O
calls	O
EndAccomplish	O
(	O
passing	O
the	O
previously	O
received	O
object	O
)	O
,	O
which	O
blocks	O
until	O
the	O
method	B-Language
has	O
completed	O
and	O
the	O
result	O
is	O
available	O
.	O
</s>
<s>
The	O
AsyncResult	O
object	O
normally	O
provides	O
at	O
least	O
a	O
method	B-Language
that	O
allows	O
the	O
client	O
to	O
query	O
whether	O
the	O
long-running	O
method	B-Language
has	O
already	O
completed	O
:	O
</s>
<s>
One	O
can	O
also	O
pass	O
a	O
callback	O
method	B-Language
to	O
BeginAccomplish	O
,	O
to	O
be	O
invoked	O
when	O
the	O
long-running	O
method	B-Language
completes	O
.	O
</s>
<s>
It	O
typically	O
calls	O
EndAccomplish	O
to	O
obtain	O
the	O
return	O
value	O
of	O
the	O
long-running	O
method	B-Language
.	O
</s>
<s>
A	O
problem	O
with	O
the	O
callback	O
mechanism	O
is	O
that	O
the	O
callback	O
function	O
is	O
naturally	O
executed	O
in	O
the	O
worker	O
thread	B-Operating_System
(	O
rather	O
than	O
in	O
the	O
original	O
calling	O
thread	B-Operating_System
)	O
,	O
which	O
may	O
cause	O
race	O
conditions	O
.	O
</s>
<s>
In	O
the	O
.NET	B-Application
Framework	I-Application
documentation	O
,	O
the	O
term	O
event-based	B-Operating_System
asynchronous	I-Operating_System
pattern	I-Operating_System
refers	O
to	O
an	O
alternative	O
API	O
style	O
(	O
available	O
since	O
.NET	B-Application
2.0	O
)	O
using	O
a	O
method	B-Language
named	O
AccomplishAsync	O
instead	O
of	O
BeginAccomplish	O
.	O
</s>
<s>
A	O
superficial	O
difference	O
is	O
that	O
in	O
this	O
style	O
the	O
return	O
value	O
of	O
the	O
long-running	O
method	B-Language
is	O
passed	O
directly	O
to	O
the	O
callback	O
method	B-Language
.	O
</s>
<s>
Much	O
more	O
importantly	O
,	O
the	O
API	O
uses	O
a	O
special	O
mechanism	O
to	O
run	O
the	O
callback	O
method	B-Language
(	O
which	O
resides	O
in	O
an	O
event	O
object	O
of	O
type	O
AccomplishCompleted	O
)	O
in	O
the	O
same	O
thread	B-Operating_System
in	O
which	O
BeginAccomplish	O
was	O
called	O
.	O
</s>
<s>
This	O
eliminates	O
the	O
danger	O
of	O
race	O
conditions	O
,	O
making	O
the	O
API	O
easier	O
to	O
use	O
and	O
suitable	O
for	O
software	B-Architecture
components	I-Architecture
;	O
on	O
the	O
other	O
hand	O
this	O
implementation	O
of	O
the	O
pattern	O
comes	O
with	O
additional	O
object	O
creation	O
and	O
synchronization	O
overhead	O
.	O
</s>
