<s>
In	O
computer	B-General_Concept
programming	I-General_Concept
,	O
event-driven	B-Application
programming	I-Application
is	O
a	O
programming	O
paradigm	O
in	O
which	O
the	O
flow	O
of	O
the	O
program	O
is	O
determined	O
by	O
events	O
such	O
as	O
user	O
actions	O
(	O
mouse	B-Device
clicks	O
,	O
key	O
presses	O
)	O
,	O
sensor	O
outputs	O
,	O
or	O
message	B-Architecture
passing	I-Architecture
from	O
other	O
programs	O
or	O
threads	B-Operating_System
.	O
</s>
<s>
Event-driven	B-Application
programming	I-Application
is	O
the	O
dominant	O
paradigm	O
used	O
in	O
graphical	B-Application
user	I-Application
interfaces	I-Application
and	O
other	O
applications	O
(	O
e.g.	O
,	O
JavaScript	O
web	B-Application
applications	I-Application
)	O
that	O
are	O
centered	O
on	O
performing	O
certain	O
actions	O
in	O
response	O
to	O
user	B-General_Concept
input	I-General_Concept
.	O
</s>
<s>
This	O
is	O
also	O
true	O
of	O
programming	O
for	O
device	B-Application
drivers	I-Application
(	O
e.g.	O
,	O
P	B-Language
in	O
USB	O
device	B-Application
driver	I-Application
stacks	O
)	O
.	O
</s>
<s>
In	O
an	O
event-driven	B-Application
application	I-Application
,	O
there	O
is	O
generally	O
a	O
main	B-General_Concept
loop	I-General_Concept
that	O
listens	O
for	O
events	O
and	O
then	O
triggers	O
a	O
callback	O
function	O
when	O
one	O
of	O
those	O
events	O
is	O
detected	O
.	O
</s>
<s>
In	O
embedded	B-Architecture
systems	I-Architecture
,	O
the	O
same	O
may	O
be	O
achieved	O
using	O
hardware	O
interrupts	B-Application
instead	O
of	O
a	O
constantly	O
running	O
main	B-General_Concept
loop	I-General_Concept
.	O
</s>
<s>
Event-driven	O
programs	O
can	O
be	O
written	O
in	O
any	O
programming	O
language	O
,	O
although	O
the	O
task	O
is	O
easier	O
in	O
languages	O
that	O
provide	O
high-level	B-Application
abstractions	I-Application
,	O
such	O
as	O
await	O
and	O
closures	B-Language
.	O
</s>
<s>
Because	O
the	O
code	O
for	O
checking	O
of	O
events	O
and	O
the	O
main	B-General_Concept
loop	I-General_Concept
are	O
common	O
amongst	O
applications	O
,	O
many	O
programming	O
frameworks	O
take	O
care	O
of	O
their	O
implementation	O
and	O
expect	O
the	O
user	O
to	O
provide	O
only	O
the	O
code	O
for	O
the	O
event	O
handlers	O
.	O
</s>
<s>
The	O
first	O
step	O
in	O
developing	O
an	O
event-driven	O
program	O
is	O
to	O
write	O
a	O
series	O
of	O
subroutines	O
,	O
or	O
methods	B-Language
,	O
called	O
event-handler	O
routines	O
.	O
</s>
<s>
For	O
example	O
,	O
a	O
single	O
left-button	O
mouse-click	O
on	O
a	O
command	O
button	O
in	O
a	O
GUI	B-Application
program	O
may	O
trigger	O
a	O
routine	O
that	O
will	O
open	O
another	O
window	O
,	O
save	O
data	O
to	O
a	O
database	O
or	O
exit	O
the	O
application	O
.	O
</s>
<s>
The	O
third	O
step	O
in	O
developing	O
an	O
event-driven	O
program	O
is	O
to	O
write	O
the	O
main	B-General_Concept
loop	I-General_Concept
.	O
</s>
<s>
Most	O
event-driven	B-Application
programming	I-Application
environments	O
already	O
provide	O
this	O
main	B-General_Concept
loop	I-General_Concept
,	O
so	O
it	O
need	O
not	O
be	O
specifically	O
provided	O
by	O
the	O
application	O
programmer	O
.	O
</s>
<s>
RPG	B-Language
,	O
an	O
early	O
programming	O
language	O
from	O
IBM	O
,	O
whose	O
1960s	O
design	O
concept	O
was	O
similar	O
to	O
event-driven	B-Application
programming	I-Application
discussed	O
above	O
,	O
provided	O
a	O
built-in	O
main	O
I/O	B-General_Concept
loop	O
(	O
known	O
as	O
the	O
"	O
program	O
cycle	O
"	O
)	O
where	O
the	O
calculations	O
responded	O
in	O
accordance	O
to	O
'	O
indicators	O
 '	O
(	O
flags	O
)	O
that	O
were	O
set	O
earlier	O
in	O
the	O
cycle	O
.	O
</s>
<s>
In	O
PL/I	B-Language
,	O
even	O
though	O
a	O
program	O
itself	O
may	O
not	O
be	O
predominantly	O
event-driven	O
,	O
certain	O
abnormal	O
events	O
such	O
as	O
a	O
hardware	O
error	O
,	O
overflow	O
or	O
"	O
program	O
checks	O
"	O
may	O
occur	O
that	O
possibly	O
prevent	O
further	O
processing	O
.	O
</s>
<s>
Exception	B-General_Concept
handlers	I-General_Concept
may	O
be	O
provided	O
by	O
"	O
ON	O
statements	O
"	O
in	O
(	O
unseen	O
)	O
callers	O
to	O
provide	O
cleaning	B-General_Concept
routines	O
to	O
clean	O
up	O
afterwards	O
before	O
termination	O
,	O
or	O
to	O
perform	O
recovery	O
operations	O
and	O
return	O
to	O
the	O
interrupted	O
procedure	O
.	O
</s>
<s>
Most	O
existing	O
GUI	B-Application
development	O
tools	O
and	O
architectures	O
rely	O
on	O
event-driven	B-Application
programming	I-Application
.	O
</s>
<s>
The	O
Java	O
AWT	O
framework	O
processes	O
all	O
UI	O
changes	O
on	O
a	O
single	B-Operating_System
thread	I-Operating_System
,	O
called	O
the	O
Event	B-Language
dispatching	I-Language
thread	I-Language
.	O
</s>
<s>
Similarly	O
,	O
all	O
UI	O
updates	O
in	O
the	O
Java	O
framework	O
JavaFX	B-Application
occur	O
on	O
the	O
JavaFX	B-Application
Application	O
Thread	B-Operating_System
.	O
</s>
<s>
Table-driven	O
state	B-Architecture
machines	I-Architecture
have	O
been	O
advocated	O
as	O
a	O
viable	O
alternative	O
.	O
</s>
<s>
On	O
the	O
other	O
hand	O
,	O
table-driven	O
state	B-Architecture
machines	I-Architecture
themselves	O
suffer	O
from	O
significant	O
weaknesses	O
including	O
the	O
state	O
explosion	O
phenomenon	O
.	O
</s>
<s>
A	O
solution	O
for	O
this	O
is	O
to	O
use	O
Petri	B-Operating_System
nets	I-Operating_System
.	O
</s>
<s>
A	O
thread	B-Operating_System
context	O
only	O
needs	O
a	O
CPU	O
stack	O
while	O
actively	O
processing	O
an	O
event	O
;	O
once	O
done	O
,	O
the	O
CPU	O
can	O
move	O
on	O
to	O
process	O
other	O
event-driven	O
threads	B-Operating_System
,	O
which	O
allows	O
an	O
extremely	O
large	O
number	O
of	O
threads	B-Operating_System
to	O
be	O
handled	O
.	O
</s>
<s>
This	O
is	O
essentially	O
a	O
finite-state	B-Architecture
machine	I-Architecture
approach	O
.	O
</s>
