Java Reference
In-Depth Information
Table 11.2
The list of events exchanged among broadcasters and listeners
Broadcaster
Event
Listener
Photocell
OCCUPIED
Controller
Photocell
CLEARED
Controller
Ticket dispenser
DELIVERED
Car, controller
Ticket dispenser
WITHDRAWN
Controller
Stopping bar
UP
Car, controller
Stopping bar
DOWN
Controller
Car
PROCEEDING
Car
to its concrete subclasses. Class
EventProcess
encapsulates an instance of
class
Thread
that executes the
run()
method of its concrete subclasses.
Clients and servers extend class
EventProcess
adding data structures and
implementing the control logic in the
run()
method.
Servers implement specific methods to handle requests of services from
the clients and to start the execution tasks. They can handle only one
request at a time and remain idle until a client invokes an interface method.
Decision point
Which communication mechanisms support the broadcaster
-
listener inter-
action pattern?
Since clients and servers encapsulate independent threads that govern
their dynamics, event scheduling cannot be centralized. In contrast to the
solution adopted for the work cell system, events are not scheduled in
advance to simulate the duration of an operation and to determine its
completion. Instead, server threads suspend their execution for the amount
of time corresponding to the duration of their operations (see the
sleep()
primitive in Sidebar 10.1, p. 255) and raise an event that notifies the
completion of that operation.
Clients synchronize with servers by suspending their execution until the
server notifies a given event. An example is the car waiting for the ticket
dispenser to deliver a ticket. The package
java.util
offers class
Observable
and
interface
Observer
to implement the server side and client side respectively
of the broadcaster
listener interaction pattern (see Sidebar 8.1, p. 181).
Class
EventProcess
inherits from both of them. In this way, every subclass of
EventProcess
can play both the roles of client and server.
Class
EventProcess
implements method
waitEvent()
in order to allow clients
to wait for specific events. Accordingly, class
EventProcess
implements
method
update()
of interface
Observer
in order to notify clients of the events
they are waiting for. This solution offers two main advantages. First, it allow
clients to use a standard implementation of method
update()
that does not
block the server thread for unspecified amounts of time (see Sidebar 8.1,
p. 181, for this issue). Second, it does not prevent clients from redefining
-