Event Driven Programming
In command-line user-interfaces and in most batch programs
there is a single input stream.
In graphical user-interfaces and process control there
are typically many input channels.
To handle input from many input channels correctly there are several
possibilities:
- Hardware interrupts may lead to the appropriate methods for handling
each input channel. This can be done if each channel is assigned a
separate interrupt.
Input from some simple peripherals on personal computers is handled this way.
The serial ports, parallel port, keyboard, mouse and clock all require
separate interrupts.
The cpu has fixed addresses where the routines for handling each interrupt
are located.
- When there are not enough interrupts, different channels may share
an interrupt. For each received interrupt an array of addresses is scanned
to find the device which has input available, and the corresponding handler
method is called.
- In graphical user-interfaces the application receives no real interrupt,
but uses an event-loop which is similar to scanning an array of
addresses of devices that share an interrupt.
In Unix, a select call is used to wait for input on a number
of input channels (sockets).
Whenever there is input, the event loop checks each input channel for
available input, and handles the first one it finds.
- When handling input events in a single thread, an event loop which
deals with keyboard and mouse events as well as file and network I/O may
not be as responsive as desired.
The behavior of several Web browsers is typical for this: while the
browser is trying to establish a network connection it may not react to
keyboard or mouse input, it may not redraw the screen when needed.
It does not even let the user stop the transfer (because it is not yet
seeing mouse or keyboard input).
- In Java the event loop is not visible to the user,
but the routine for finding out the input channel and the method to call
may be visible. There are three ways to deal with events in Java:
- The lowest level event handler is the
handleEvent
method.
- The common JDK 1.0.2
event handling distinguishes types of events, but still requires the
Java program to determine the source of an event.
- The newer JDK 1.1
event handling uses explicit senders of and listeners to events.