This section shows you how to use the threaded behaviour that is provided by the JeLSIM toolkit to produce simulations that change over time. The model and interface used in this section can be downloaded here: trainingTime.zip.
The applet below shows a simple example of a bomb counting down from 10 to 0. It's linked to an nxy image to show how you can create basic animation based on time-varying variables.
To produce this type of behaviour, you must provide up to three additional methods in your model code when you are creating your model. These methods are provided in the superclass (
mainSimClass) but can be overridden in your own code to provide the behaviour you want for your thread. These three methods are:
public void runInitialise()
public boolean runCheck()
public void runCalc()
If you are using the Model Wizard, this is the screen where you will be given the option to include one or more of these methods in your code.
Think of the thread as a loop running within your simulation that can be started and stopped, in the example above there is a counter that increments every iteration of the loop until it has reached a maximum (the countdown behaviour is achieved by linking this counter to another counter working in the opposite direction).
This method is called when the thread is started or restarted. In our example it sets the counter to zero.
This method is called on every iteration of the loop to see whether the loop will be repeated again. If the return value is true, the loop will repeat again. In the example this method checks to see whether the counter is greater than the maximum.
This method is called once for every iteration of the thread loop and contains the main body of functionality. In the example the counter is incremented in this method (and the value of the downwards counter re-calculated).
JeLSIM threads also inherit four methods that can be used to control them. These methods do not have to be overridden but do have to be registered before they can be accessed at the user interface.
start creates the thread and starts it running.
stop stops the thread running.
pause will pause the thread and calling again will restart it.
restart will restart the thread once it is running (note that the runInitialise method will be called when a thread is restarted).
Changing the running rate
At the end of each iteration of
runCalc, the thread will wait a pre-specified amount of time before carrying out the next iteration. You can set the delay using the inherited method
setLoopDelay(int i) where
i is the delay in milliseconds. In the example, the delay integer is registered as a property and can be changed by the end user to slow down or speed up the thread rate.