以下的Scheduler的實現參考自Marshall "Game Programming Gems 3"中的C++代碼:
package game.engine.core;
public class Scheduler {
// clock:
private Clock clock = new Clock();
// 啟動Scheduler:
public void start() {
clock.start();
}// 停止Scheduler:
public void stop() {
clock.stop();
}public int getSystemTime() {
return clock.getSystemTime();
}public int getVirtualTime() {
return clock.getVirtualTime();
}
// 執行完整的一幀:
public void executeFrame() {
System.out.println("-- start execute frame --");
clock.beginFrame();
int started = clock.getSystemTime();
// do time task:
System.out.println("doing time tasks...");
try {
Thread.sleep(500);
}catch(InterruptedException ie) {}
clock.advanceToEnd();
// do frame task:
System.out.println("doing frame tasks...");
try {
Thread.sleep(200);
}catch(InterruptedException ie) {}
// do render task:
int end = clock.getSystemTime();
int elapsed = end - started;
int frameLength = clock.getFrameEnd() - clock.getFrameStart();
System.out.println("elapsed: " + elapsed + ", frame: " + frameLength);
System.out.println("cpu usage: " + (elapsed * 100 / frameLength) + "%");
// cleanup:
System.out.println("-- end execute frame -- ");
}