import org.sefirs.*;
public class Example {
/**
* An example of a simulated thread. A Sefirs SimulatedThread
* works exactly like a regular java.lang.Thread (it is actually a
* subclass of java.lang.Thread), except it runs in simulated
* time. This specific class will print out the current time on
* every even second.
*/
private static class Even extends SimulatedThread {
public Even(Simulation simulation) {
super(simulation, "Even");
}
public void run() {
while(isAlive()) {
System.out.println("Even: " + getProcess().getSimulation().currentTime().expand());
try {
sleep(2000);
} catch(InterruptedException ie) {
System.out.println(ie.toString());
}
}
}
}
/**
* An example of a process. A process is a primitive within
* Sefirs, upon which the Sefirs SimulatedThread is built. This
* specific class will print out the current time on every odd
* second.
*/
private static class Odd extends SimulatedProcess {
public Odd(Simulation simulation) {
super(simulation);
}
public void run() {
while(isAlive()) {
sleep(new Seconds(1));
System.out.println("Odd: " + getSimulation().currentTime().expand());
sleep(new Seconds(1));
}
}
}
public static void main(String[] args) {
Simulation sim = new Simulation(new Seconds(1));
Even e = new Even(sim);
SimulatedThread o = new SimulatedThread(new Odd(sim));
e.start();
o.start();
sim.runUntil(new Days(1));
System.exit(0);
}
}
syntax highlighted by Code2HTML, v. 0.9.1