sclass FlexibleRateTimer implements AutoCloseable { double hertz; transient Runnable action; transient double timestamp; transient DoLater doLater; transient new Flag started; transient volatile bool disposed; *() {} *(double *hertz) {} *(double *hertz, Runnable *action) {} void start { if (!started.raise()) ret; _kaboom(); } void _kaboom { if (disposed) ret; dispose doLater; if (timestamp == 0) timestamp = sysNow(); timestamp += 1000/getFrequency(); doLater = new DoLater(iround(timestamp), r { pcallF(action); _kaboom(); }); doLater.enable(); } void cancel { close(); } public void close { set disposed; dispose doLater; } void setRunnableAndStart(Runnable action) { this.action = action; start(); } // takes affect _after_ next trigger synchronized void setFrequency(double hertz) { this.hertz = hertz; } synchronized double getFrequency() { ret hertz; } }