I am trying to execute couple of instructions or a function N times in one second. How can i do this in java? As follows...
//in one second
while(N)
{
printf(".........");
int x=0;
printf("The value of x is ");
}
but the question actually goes little deeper.. I am trying to plot pixels manually and I want the no of rotations per second effect... so basically, it has to execute N times for a second (But this is done infinitely )
thanks in advance
You can never be sure it will happen exactly N times per second, but it goes like this:
long taskTime = 0;
long sleepTime = 1000/N;
while (true) {
taskTime = System.currentTimeMillis();
//do something
taskTime = System.currentTimeMillis()-taskTime;
if (sleepTime-taskTime > 0 ) {
Thread.sleep(sleepTime-taskTime);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With