class Procedure // or Runnable { fun void run() { // empty } } class FloatProcedure { fun void run(float arg) { // empty } } class Looper { 0::ms => static dur none; fun static void loop(Procedure procedure, dur wait) { loop(procedure, none, wait); } fun static void loop(Procedure procedure, dur offset, dur wait) { offset => now; while (true) { procedure.run(); wait => now; } } fun static void loop(FloatProcedure procedure, dur wait, dur length) { loop(procedure, none, wait, length); } fun static void loop(FloatProcedure procedure, dur offset, dur wait, dur length) { offset => now; dur expired; while (expired <= length) { expired / length => float ratio; procedure.run(ratio); wait => now; wait +=> expired; } } } // testing below... class Tick extends Procedure { fun void run() { <<<"tick">>>; } } class Print extends FloatProcedure { fun void run(float arg) { <<>>; } } Tick tick; Print print; // static methods // run tick every second //loop(tick, 1::second); // run tick every second after waiting five seconds //loop(tick, 5::second, 1::second); // run print every second for a total of five seconds //loop(print, 1::second, 5::second); // run print every second for a total of five seconds after waiting five seconds //loop(print, 5::second, 1::second, 5::second); // spork a new shred to run print every second for a total of five seconds after waiting five seconds //spork ~ loop(print, 5::second, 1::second, 5::second); // spork several shreds to run print every second for a total of five seconds after waiting between two and eight seconds //spork ~ loop(print, 2::second, 1::second, 5::second); //spork ~ loop(print, 4::second, 1::second, 5::second); //spork ~ loop(print, 6::second, 1::second, 5::second); //spork ~ loop(print, 8::second, 1::second, 5::second); // wait for sporked threads to finish //20::second => now; // ...or with instance of Looper Looper looper; // run tick every second //looper.loop(tick, 1::second); // run tick every second after waiting five seconds //looper.loop(tick, 5::second, 1::second); // run print every second for a total of five seconds //looper.loop(print, 1::second, 5::second); // run print every second for a total of five seconds after waiting five seconds //looper.loop(print, 5::second, 1::second, 5::second); // spork a new shred to run print every second for a total of five seconds after waiting five seconds //spork ~ looper.loop(print, 5::second, 1::second, 5::second); // spork several shreds to run print every second for a total of five seconds after waiting between two and eight seconds spork ~ looper.loop(print, 2::second, 1::second, 5::second); spork ~ looper.loop(print, 4::second, 1::second, 5::second); spork ~ looper.loop(print, 6::second, 1::second, 5::second); spork ~ looper.loop(print, 8::second, 1::second, 5::second); // wait for sporked threads to finish 20::second => now;