Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

imit_model / AnyLogic / UsersManual(AnyLogic)

.pdf
Скачиваний:
125
Добавлен:
06.06.2015
Размер:
4.28 Mб
Скачать

AnyLogic V User’s Manual

Figure 186. Options dialog box. Build page

On the Build page of the Options dialog box, you specify paths to the Java compiler, Java virtual machine and packager used and optionally some compiler and Java VM options.

Compiler executable file name - the full path to Java compiler, e.g. C:\j2sdk1.4.0\bin\javac.exe.

Java VM file name – the full path to Java virtual machine dynamic link library (e.g. C:\j2sdk1.4.0\jre\bin\client\jvm.dll).

Packager – the full path to Java packager (e.g. C:\j2sdk1.4.0\bin\jar.exe).

Engine library file name – the full path to AnyLogic simulation engine library

(e.g. C:\Program Files\XJ\AnyLogic5.0\Lib\xjanylogic5engine.jar).

Compiler options – [optional] options passed to Java compiler. If JDK compiler is used, compiler options are usually not needed.

© 1992-2004 XJ Technologies http://www.xjtek.com

427

Chapter 22. Project building. Model initialization and termination order

Java VM options – [optional] options passed to Java virtual machine (e.g. “–Xmx80M” sets Java heap size to 80MB). If JDK compiler is used, virtual machine options are usually not needed.

22.2 Model initialization and termination order

The information presented in this section might be useful for better understanding of how a model initializes and terminates in terms of ordering of various operations.

22.2.1Model initialization order

The order of operations during initialization is the following:

1.Time is set to 0

2.The root object of the model is constructed, its parameters are being set, and its method create() is called. The method create() of each active object does the following steps for each encapsulated object:

3.Constructs the encapsulated object

4.Sets up the encapsulated object parameters

5.Calls its method create()

6.Calls its animation method setup(), if any

At this point creation of the model and the animation is completed.

7.The method startup() for each object is called. The method is called on the

innermost objects first and then up along the active object tree. During the method startup() each object creates and initializes its statecharts (each statechart enters its initial state) and timers

8.The global equation set is evaluated for the first time

9.The animation method update() is called for the first time.

428

© 1992-2004 XJ Technologies http://www.xjtek.com

AnyLogic V User’s Manual

22.2.2Model termination order

The model destruction is performed in the following order:

1.The model destruction starts at the root object, where the method destroy() is called. For each active object the method destroy() performs the following steps:

2.The user-defined method cleanup() is called

3.All activities (statecharts, timers, etc) are stopped

4.The method destroy() is called for all encapsulated objects recursively

5.All active object elements (ports, variables, activities, animation, etc) are deleted

6.The method onDestroy() is called for the active object.

Since the method cleanup() is called prior to the recursion, it is first called for root object and then for all object down the tree. On the other hand, the method onDestroy() is called after the recursion, therefore it is first called on leaf objects and then up the tree.

© 1992-2004 XJ Technologies http://www.xjtek.com

429

Chapter 23. Threads

23. Threads

You can implement an activity in a Java method and run it in a separate thread within an active object. Threads are less visual than statecharts or timers, but in rare cases, they may provide for more natural representation of some algorithms. In general, however, if the activity has a set of states with different reactions to events, make it a statechart.

If there are no synchronization operations in the behavior (it is a pure computation), then you can call it from other places; e.g., from ports or from actions of states and transitions, and you do not need to run it in a separate thread.

To run an activity implemented in a Java method, you need to:

Define method of an active object class with the return type void. This method will run as thread. The method can be defined e.g. in the code section Additional class code.

Call method startThread() of the active object supplying the name of the defined method as a string.

The thread runs concurrently with all other activities (statecharts, timers). If it does not wait for anything, it terminates taking zero model time.

AnyLogic provides the following API to work with synchronization and time (for more information please consult AnyLogic Class Reference):

Related methods of ActiveObject

void startThread( String methodName ) – creates a new simple thread based on the method name of this ActiveObject.

void delay( double timeout ) – suspends the thread for the specified amount of time.

Related methods of PortQueuing

Object waitForMessage() – suspends the thread until there is a message in the port queue. Extracts the message and returns it.

430

© 1992-2004 XJ Technologies http://www.xjtek.com

AnyLogic V User’s Manual

Object waitForMessage( double timeout ) – same as above, but with a timeout. On timeout returns null.

Example

In the example shown in Figure 187 the thread performs an infinite loop: it waits for the message arrival, then makes 10 time units delay.

 

Startup code:

 

startThread( "funcActivity" );

MyClass

Additional class code:

public void funcActivity() {

port

traceln( "Start" );

while( true ) {

 

traceln( "Step 1" );

 

port.waitForMessage();

 

traceln( "Step 2" );

 

delay( 10 );

 

}

 

}

 

Figure 187. Thread

© 1992-2004 XJ Technologies http://www.xjtek.com

431

Соседние файлы в папке AnyLogic