Java Reference
In-Depth Information
As mentioned earlier, for this example we are also using the simple technique of
returning only the most recent data stored in SimData , allowing any intervening
data generated by the computation to be lost if the client does not poll often
enough. More elaborate schemes can be devised, such as storing intermediate
data in SimData and returning a collection of results, but doing so now would
distract from the basic client/server design being demonstrated here.
20.5.2 The SimulationThread class
Continuing around the collaboration diagram in a clockwise direction, which
is roughly the order in which the ojects are encountered, the next class to
discuss is the SimulationThread class. It is a thread so it extends java.
lang.Thread :
public class SimulationThread extends Thread { ... }
When SimulationThread is constructed (at sequence 2 in the collabora-
tion diagram), we see that sequence 2.1 also must be performed to construct a
Simulation object. So the SimulationThread constructor looks like:
public SimulationThread (SimData sim - data) {
fSimulation = new Simulation (sim - data);
}
where we have passed the SimData object on to the Simulation constructor
as shown in the collaboration diagram.
This thread's run() method starts the simulation running by calling the
startRunning() method on the Simulation object. We can note from the
collaboration diagram that startRunning() is a synchronous call. That is, it
blocks until the calculation is complete, at which time run() exits.
public void run () {
fSimulation.startRunning ();
}
Recall that the best and proper way to end a thread is to set a boolean value
that the running thread checks periodically. In this case, our run() method is
not in a loop itself, but has called Simulation.startRunning() instead. If
the server ever needs to halt the simulation, perhaps on command from the client
(through additional remote methods that we do not show here), it is a good idea
to provide a halt() method that passes the halt command on to Simulation :
void halt () {
fSimulation.halt ();
}
Search WWH ::




Custom Search