Reactive Programming
The fair framework offers a way to conciliate efficiency and
programming ease through the use of reactive programs. Reactive
programs are basic elements of the Junior framework [10] which
is a set of Java classes for reactive programming. Junior programs are
usable through an API called Jr [9]. Comparison of threads and of
SugarCubes, a framework closely related to Junior, can be found in
[7]. The Web site [3] contains references to
reactive programming.
Reactive programming is based on the notion of an instant which is
shared by all concurrent components. Reactive programs are made of
basic instructions, with semantics defined in term of instants. For
example, the instruction Stop()
of Junior stops execution for the
current instant, and execution at the next instant restarts in sequence
from it. Event based programming is possible in Junior; events are
instantaneously broadcast: an event generated during one instant is
received by all components waiting for it during the same instant.
Correspondance between fair threads and Junior is straigtforward: a
Junior instant corresponds to a fair scheduler phase, where all
threads are executed once; events have exactly the same semantics in
the two contexts; the cooperate()
method of fair threads
corresponds to the Stop()
instruction of Junior.
Junior programs can be used in the context of fair threads, by the
intermediate of class JrProgWrapper
which extends
FairThread
. For example, consider the class:
public class Kill extends Until
{
String msg;
public Kill(String s){ super("E"); msg = s; }
public void controled(FairScheduler scheduler){
for(int i = 0; i < 100; i++){
System.out.print(msg+"init ");
cooperate(5);
System.out.print(msg+"end ");
}
}
}
The equivalent class, made of a reactive program of type
Program
, is the following:
public class Kill extends JrProgWrapper
{
Program prog =
Jr.Until("E",
Jr.Repeat(100,
Jr.Seq(Jr.Atom(new Print(msg+"init ")),
Jr.Seq(Jr.Repeat(5,Jr.Stop()),
Jr.Atom(new Print(msg+"end "))))));
public Kill(){ super(); setProgram(prog); }
}