Java Reference
In-Depth Information
derive the mass. Since this is a discrete simulation, we also need to know the time
step to use. It is also convenient to specify a maximum run time at which point
the simulation will end. We arbitrarily choose the order of elements to be
A
,
ω
,
ω
K
,
T
, and
T
max
.From
K
and
we calculate
m
.Sothe
initialize()
method
becomes
public void initialize (float[] indata) {
fAmplitude = indata[0];
fOmega = indata[1];
fSpringCons = indata[2];
fDeltaT
= indata[3];
fMaxTime
= indata[4];
fMass
= fSpringCons/(fOmega*fOmega);
}
Notice that the size of the array received in the parameter list must be at least five.
If it is longer, the server just ignores the rest of the elements. Our agreement is only
that the first five elements represent the values of the physical parameters as cho-
sen. We use this fact shortly. (If the input array is shorter than five, there will be an
ArrayIndexOutOfBoundsException
on the server.) Next let's implement
the simple
halt()
method to be called by
SimulationThread.halt()
.It
merely sets a
boolean
that is checked periodically by the running simulation. In
a real-world example, there may be other clean-up tasks that should be performed
here, such as closing a connection to a database, etc.
public void halt () {
fKillMe = true;
// Additional clean-up tasks
...
}
At sequence 4, upon command from the client, the
Server
object calls
SimulationThread
.
start()
,which begins the thread's
run()
method,
which was implemented above to call
Simulation.startRunning()
at
sequence 4.1. Our collaboration diagram shows that
startRunning()
actu-
ally passes the call on to the
Compute
object in sequence 4.2. The purpose
of the
Compute
object is to separate the actual numerical computation from
SimulationThread
, thereby permitting the use of legacy code via the Java
Native Interface (JNI). We discuss JNI in Chapter 22 where we show how to call
native methods in an external library. This example uses no legacy code, plus it
is simple enough that the entire code can easily fit inside
SimulationThread
,
so we won't actually be using a
Compute
object. Therefore, all our numerical
work appears inside
startRunning()
.
Search WWH ::
Custom Search