I'd like to write an Android application (a game, to be exact), that needs to perform certain actions at specified time. The precision of those updates should be not lower than 1ms. Is that possible?
I've tried running a thread and measure the time between updates using the System.nanoTime(). In result, there are hundreds of times when the time between updates was equal or longer than 1ms.
Can I somehow achieve a precision which would assure me that there's at least one loop executed per each 1ms?
Here's the code I've used for my test:
public class MyThread extends Thread
{
private boolean running = false;
public void setRunning(boolean running)
{
this.running = running;
}
public MyThread()
{
this.setPriority(MAX_PRIORITY);
}
@Override
public void run()
{
long currentTimeNano = 0;
long lastFrameTimeNano = 0;
long nanoFrameDelay = 0;
int longDelays = 0; //Number of delays >= 1ms
while(running)
{
currentTimeNano = System.nanoTime();
//Measure delay between updates in ns
nanoFrameDelay = currentTimeNano - lastFrameTimeNano;
if(nanoFrameDelay >= 1000000)
longDelays++;
lastFrameTimeNano = currentTimeNano;
}
}
}
Thank you in advance!