Hi, thanks for reading,
I am doing some basic ‘research’ and i’ve written an Applet that animates a ball and moves a paddle using MouseMotionListener. The Applet implements Runnable, i got this idea from a tutorial somewhere, i say this because i’m not entirely sure how that’s working. I know it fixed the problem i had where [i guess] the Applet Thread hogged the CPU and the IO was never processed.
My problem is that there are blips in performance. I can see the blips visually as well in the output on the console. I say output the currentTime - endOfLastIterationOfLoop, that’s probably confusing so let me give it to you in pseudo code:
time1 = System.currentTimeMillis (); // for first iteration.
while (true) {
ball.animate ();
time2 = System.currentTimeMillis();
System.out.println (time2-time1);
time1 = System.currentTimeMillis();
repaint();
}
so the output is something like
…
…
…
0
0
0
0
0
0
15
0
…
…
…
I tried using Thread.sleep(), inserting a 10ms delay, which results in:
16
0
31
16
15
16
Mostly it’s 16s and 15s but that occasional 31 bothers me. I suppose this is Garbage Collection?
Actually those zeros bother me too. Is this a matter of System.out.println (timeDiff) rounding the long value to zero? Or is this value actually zero? If the Thread is supposed to sleep why doesn’t it? I understand it goes long because the timer on XP is ~15ms. And the 31ms delay seems to be ~double the 15 or 16.
I suppose i should do a test myself, but is this behaviour similar in an Application / WebStart ?
Would something like http://javolution.org/help?
And what about how this problem scales? If/When i have a large number of objects to animate and more IO to accept and more game logic to compute, what happens then? To me it seems it would get worse.
EDIT: thanks again!