I have a program that needs to execute a process at a very precise time. The way I have currently approached it is:
while( ( exTime - System.currentTimeMillis() ) > randomNum )
{
try
{
Thread.sleep(0, 5000); //Sleep for 5000 nanoseconds, or 0.000005 seconds
}
catch (Exception e)
{
e.printStackTrace();
}
}
//Code to be executed after time is over.
I know it is also possible to use a Java timer for this.
My question is what is the most efficient, and what is most accurate? I am running over 100 threads with the while-loop. Will using a timer save more resources and be more accurate?
Using a Timer will be accurate. For resources you can check
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.