Reactive Engines for Concurrent Programming


Concurrency

In concurrent executions, several programs run "together", and not "in turn" as in sequential ones. Java offers threads for concurrent programming. However, there is an efficiency problem with threads. For example, the threaded version of the preys-predators demo is less efficient than the version without them.

Threads are often rejected and replaced by programming approaches in which concurrent activities are split into small elementary steps run in turn. The reactive approach is an example of such programming technique and it proposes an execution engine for concurrency which does not use threads. The aim of the following demos is to show the benefit one can expect from using it.


Behaviours Synchronisations

In many cases, one needs to synchronise behaviours of objects that are run in parallel. This is the case for discrete time simulations of physical phenomena: objects are changing during each time interval dt and the overall behaviour is the sum of behaviours of all the small objects present in the system.

The planet-meteor demo simulates gravity. Implicit synchronisation provided by the reactive engine at each end of instant is crucial for being able to put meteors in orbits around the planet. Using some thread-based technique without implementing equivalent synchronisations would produce impredictable results.


Determinism

An important point is that reactive engines are deterministic, which is not the case for threads. In particular, results do not depend on the used execution platform caracteristics (cooperative or preemptive).

The PlanetMeteor is made of two planets; each planet behaves also as a meteor and is thus attracted by the other planet; one gets a system in which the two planets are moving around the other. Due to initial conditions, the two planets are slowly getting closer, and finally they are ejected from the frame. As initial conditions are fixed and because the reactive engine is deterministic, the produced drawing is always the same.


Keeping Coherency

Several activations during the same instant are sometime needed to get a coherent reaction of all system components. The rectangle demo shows how the shape of a composite rectangle made of several little circles can be maintained over the time, despite the global moves of the rectangle.


Communications

The components activation strategy must, in some cases, be a dynamic one; that is, it must be able to change at run time. This is for example the case in the energy transfer demo in which energy is transfered during collisions.


Dynamicity

There are cases where the number of parallel components dynamically changes and is not a priori fixed. The Traffic Jam consists in cars that are running around a rectangle track; cars are dynamically introduced on the track; too much cars generates traffic congestions.

The Sine+cosine=circle shows small points that are moving together following sine and cosine pathes.


Fine Control over Execution

The Termites demo shows termites that cooperate to stack food packs in a unique place. Termites are all killed when a "flytox" event is generated. This is a safe way to destroy objets (compared with the Thread.stop Java method, now deprecated).