Will Thread priority increases accuracy of Thread.sleep(50);?
As we know Threads aren’t accurate when you call sleep for 50ms, But does it increases accuracy by any mean? If thread is listed as MAX_PRIORITY.
Will be thankful for any kind of explanation.
The accuracy of sleep is down to the operating system. If you want greater accuracy you can use another OS. Another approach is don’t sleep, you can busy wait instead. Or you can sleep for say 45 ms and busy wait for 5 ms.
If you have a task which you need to run 20 times per second, you are better off keeping track of when the tasks should run next and run it at the time (rather than wait a fixed amount of time) as what you do between sleeps will also take some time.
BTW This is what ScheduledExecutorService.scheduleAtFixedRate does for you.
It can sleep between task with micro-second accuracy (assuming the OS supports it) but it tries not to drift.