Java Reference
In-Depth Information
public void
attachTo(EventProcess process) {
process.addObserver(
this
);
}
// notifies this process that "observable" raised "event"
public synchronized void
update(Observable observable,
Object event) {
// records the name of the observable and of the event
// in member variable;
this
.observable
#
observable.toString();
this
.event
#
(String) event;
// wakes up the thread that is waiting for an event
notify();
}
// the waitEvent() method is invoked by an Observer
// object; it suspends this thread until process
// "p_observable" raises event "p_event"
public synchronized void
waitEvent(Observable observable,
String event) {
// verifies that the event has been raised
while
(! (
this
.observable.equals(observable.toString()) &&
this
.event.equals(event)))
try
{
// the thread suspends its execution until the
// notify() method has been invoked
this
.wait();
}
catch
(InterruptedException ie)
{ ie.printStackTrace();}
// reset the event; this allows the process to wait for
// a new event of the same type
this
.event
#
"";
}
// this method is called by an Observable object;
// it notifies the observers that its state has changed
public void
notifyObservers(String event) {
this
.setChanged();
super
.notifyObservers(event);
}
// starts this process
public void
start() {
// creates a thread object and starts it
thread
#
new
Thread(
this
);
if
(! alive) {
alive
#
true
;
thread.start();
}
}