Updated by
Modified
SARL-FAQ.markdown- Ignore whitespace
The `on Initialize` behavior handler in agents is a bit special, as it is the code ran when an agent is born. As such, its execution is more "synchronous" than other on-behavior rules. In particular:
1. Any event emitted within an `on Initialize`, will not be processed until that `on Initialize` code finishes. So, your agent initialization should _not_ depend (and wait) on any emited event being processed, as they won't!
-2. When spawning an agent in `on Initialize`, the spawn instructions will not return until all the `Initialize` behaviors of the spawned agent have been executed fully.
+2. When spawning an agent in `on Initialize`, the spawn instructions will return only after the agent has been created. However, creation of the agent (i.e., of the corresponding object) doe snot include initialization of the agent via its `on Initialize` behaviors. Said so, the Java thread manager may process those initialization processes of the new agent before continuing with the execution of the spawnning agent (and this seems to be the case in many Linux boxes where the executor service of Java tends to have the same behavior during all the runs). If you change computer, it may be different (I have encountered this fact with my students). In the following example, the thread executor service of Java seems to give the priority to the "`on Initialize`" of agent 2 instead of continuing the run of the spawn function.
+Here it appears as the "`on Initialize`" behaviors have been run all before the execution resumes after the `spawn()` statement, but this is just one way and one should not rely on that behavior being guaranteed: once the spawned agent is created, the `spawn()` commands returns.
The function `fire(e)` retrieves all the "`on E`" and runs them in parallel, and there is a synchronization point after the running of all the "`on E`" if `E` is `Initialize` or `Destroy` (for forcing synchronous execution of "`on Initialize`" and "`on Destroy`"). At the end of the "`on Initialize`" (after synchronization point), all the buffered events are fired.
-Regarding `spawn()`, the function runs the initialization process within a _separated thread_. In other works, the caller of `spawn()` is never blocked by the call. Then, the created thread runs all the initialization process, including the synchronous execution of "`on Initialize`".
+Observe that if the event is fired from within the "`on Initialize`", the same algorithm is applied whatever the receiving agent.
+1. First, the spawn agent is created. This part is run _in the same thread_ as the caller of spawn, so the spawn call _blocks_.
+2. Once the spawn agent has been created, the initialization process runs within a _separated thread_ from the spawner agent. So, the call `spawn()` is now not locked anymore. Then, the created thread runs all the initialization process, including the synchronous execution of "`on Initialize`". Consequently, the "`on Initialize`" of the spawn agent will not block the spawn caller.