0

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.

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by Mathukutty
0

Is your OS multi tasking? Can the OS use time while your code is executing?

tm.getCurrentThreadUserTime()

What class is the tm object?

0

Yes its a multitasking OS.
tm is of type ThreadMXBean.

I wouldn't have been surprised if milli gave zero and nano gave nonzero value. Unfortunately its the other way round.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.