|
![]() |
Active objects, as indicates their name, have an activity of their own (an internal thread).
It is possible to add pre and post processing to this activity, just by implementing the interfaces
InitActive and EndActive, that define the methods initActivity and endActivity.
The following example will help you to understand how and when you can initialize and clean the activity.
When instanciated, the activity of an object is automatically started, but it will first do what is written in the initActivity method.
Ending the activity can only be done from inside the active object (i.e. from a call to its own body). This is the reason why we have written a terminate method in the following example.
The InitializedHello class extends the Hello class, and implements the interfaces InitActive and EndActive.It acts a a server for the InitializedHelloClient class.
The main method is overriden so that it can instantiate the InitializedHello class
The source code of the InitializedHello class is here.
initActivity and endActivity here just log messages onto the console, so you can see when they are called.
initActivity is called at the creation of the active object, while endActive is called after the activity has terminated (thanks to the method terminate).
Here is the initActivity method :
public void initActivity(Body body) { System.out.println("I am about to start my activity"); }
Here is the endActivity method :
public void endActivity(Body body) { System.out.println("I have finished my activity"); }
The following code shows how to terminate the activity of the active object :
public void terminate() { // the termination of the activity is done through a call on the // terminate method of the body associated to the current active object ProActive.getBodyOnThis().terminate(); }
Code is here.
The only differences from the HelloClient of the previous example is the class instantiated, which is now InitializedHello (and not Hello), and you will add a call to hello.terminate(). The source code is here.
Execution is similar to the previous example; just use the InitializedHelloClient client class.