Java Reference
In-Depth Information
we indicate the creation of the new
simData
object with the
create()
method.
In practice, in Java, the
create()
is actually implemented as a Java
new
opera-
tion.) In sequence 2, a
SimulationThread
object is created. This is the thread
that runs the calculation. During the
SimulationThread
constructor (i.e. dur-
ing sequence 2), a new
Simulation
object is created at sequence 2.1 and then a
new
Compute
object at sequence 2.1.1. The
Compute
object is broken out sepa-
rately for convenience, although its operations could be rolled into
Simulation
.
We e xplain
Compute
in more detail shortly. In practice, the construction of
Simulation
and
Compute
may be done during the
SimulationThread
constructor as shown, or it may be postponed until the thread starts running
during sequence 3. There is no particular advantage either way.
Observe that
Simulation
has an association with the
SimData
object -
i.e. it must call a
SimData
method. Thus
Simulation
needs a reference to
the
SimData
instance. By using the parameter list features of the UML mes-
sages, we've explicitly shown how the
simData
reference gets communicated to
Simulation
.Eventually, though not shown here, the client instructs
Server
to start running the simulation. At this time, in sequence 3, the
Server
sends a
start()
message to
SimulationThread
,which begins the thread's
run()
method in the normal Java fashion. That is, the
run()
method in
SimulationThread
begins execution in another thread of control, and the
start()
message returns immediately. Because
start()
returns immediately
while the other thread is still running, this is an asynchronous method invocation.
Asynchronous messages are indicated in UML with half arrowheads as shown.
Sequence 3 is fairly complicated, with several sub-sequences as shown. At
3.1, the
Simulation
's
startRunning()
method is called, which calls on the
Compute
object to do the actual calculation at 3.2. During the computation, the
Compute
object periodically calls
storeData()
at 3.2.1, marked as an itera-
tion since there are several such calls over the course of the calculation. When
Simulation
receives the
storeData()
call, it is forwarded to
SimData
at sequence 3.2.1.1, and the data is stored in the
SimData
object. Finally,
when the calculation is complete, we show an explicit “finished” message from
Compute
to
Simulation
. This is actually a return from successful completion
of sequence 3.2, but it is shown explicitly because of the complexity of sequence 3.
Return messages are shown in UML as dashed arrows. When
Simulation
receives the return message, it is finished as well, and another finished message is
shown returning to
SimulationThread
in sequence 3.4. That finished message
corresponds to the end of the thread's
run()
method.
Much simpler is sequence 4 in which the client has requested data from
Server
, and
Server
requests data from
SimData
with a
getData()
mes-
sage. At this point, the data stored in
SimData
is returned to
Server
and is then
forwarded to the client.
The reason that
Compute
is broken out as a separate object is because the cal-
culation is often performed in legacy code, and that legacy code usually amounts
Search WWH ::
Custom Search