Move LoopingRunnable, begin work on main and VisionManager

This commit is contained in:
Banks Troutman
2019-11-16 14:30:44 -05:00
parent 58c1924c7e
commit 164a02159e
9 changed files with 115 additions and 89 deletions

View File

@@ -0,0 +1,33 @@
package com.chameleonvision.classabstraction.util;
/**
* A thread that tries to run at a specified loop time
*/
public abstract class LoopingRunnable implements Runnable {
private final Long loopTimeMs;
protected abstract void process();
public LoopingRunnable(Long loopTimeMs) {
this.loopTimeMs = loopTimeMs;
}
@Override
public void run() {
while(!Thread.interrupted()) {
var now = System.currentTimeMillis();
// Do the thing
process();
// sleep for the remaining time
var timeElapsed = System.currentTimeMillis() - now;
var delta = loopTimeMs - timeElapsed;
if(delta > 0.0) {
try {
Thread.sleep(delta, 0);
} catch (Exception ignored) {}
}
}
}
}