In the code below, I don't understand why the diference in elapsed time calculated with System.currentTimeMillis() is different tm.getCurrentThreadUserTime()
The elapsed time in millisecond is always a value like 23 or 14 while time calculated with tm.getCurrentThreadUserTime() is always 0 . I know one is milli and the other is nanosecond. Even then why is it 0?
public void animationLoop() {
long startTime = System.currentTimeMillis();
long currTime = startTime;
int count = 0;
ThreadMXBean tm = ManagementFactory.getThreadMXBean();
long cpuTime = 0;
long cpuTimeElapsed = 0;
while (currTime - startTime < DEMO_TIME) {
long elapsedTime = System.currentTimeMillis() - currTime;
cpuTimeElapsed = tm.getCurrentThreadUserTime() - cpuTime;
//currTime += elapsedTime;
currTime = System.currentTimeMillis();
cpuTime = tm.getCurrentThreadUserTime();
System.out.println("CPU Time: "+ cpuTimeElapsed +" Elapsed Time: "+elapsedTime);
// update animation
anim.update(elapsedTime);
// draw to screen
Graphics g = screen.getFullScreenWindow().getGraphics();
draw(g);
g.dispose();
System.out.println("Iteration: "+ count+ " Elapsed time: " + elapsedTime);
}
}
This code is taken from Brackeen's Game programming in Java.