Why is Thread.sleep() inherently inaccurate
Avi Ribchinsky, a friend and a college of mien, is transitioning from C++ to the Java world. He had been playing with Thread.sleep(), when he noticed that the sleep method might oversleep more than ordered, and moreover, it could also under sleep (see Fig 1). Coming from the C++ world, that surely caught him surprised
Fig 1.
How is sleep implemented in Java anyway?
Avi came asking me if I knew anything about it, I was wondering myself how such a common and important method could be faking in the way shown above. Is it the OS? a Bug in the specific JRE version used? Maybe the API doesn’t guarantee milliseconds precision to begin with?
Thinking about all of these factors, we realized that we don’t really know how the JVM implements the sleep method functionality, my best guess would have been that the process registers itself in the OS for a wake up call, and the OS wakes the process via a software interrupt. OK, time to search the web.
The following article gives a very detailed answer, explaining that sleep is implemented by a thread giving up its OS scheduling quantum back to the scheduler, on the next execution quantum the thread gets, it has the chance to wake up and continue processing, or again continue sleeping.
Therefore, the accuracy resolution of sleep is directly dependent on the process scheduling resolution of the operating system in usage. Since windows XP process scheduling resolution is roughly 10ms, the sleep mechanism, in the Avi’s example, might had preferred to under sleep “a little” rather than oversleeping “a lot”, by waking himself in the current scheduling cycle quantum, rather than in the next, future, quantum.
The article also mentions that the inaccuracies are worsened when a process with a higher scheduling priority, than the sleeping process, is in a runnable state.
I assume that, running on a Hypervisor with course grained process scheduling would also produce greater inaccuracies.
Conclusion
You can’t rely on the millisecond accuracy of the sleep method. Take a before and after time measurament to find the actual time spent sleeping, in order to avoid ever increasing inacurracies.
Sleep tight
|
This entry was posted by Gili Nachum on August 23, 2009 at 21:06, and is filed under general, hardware, Multi Threading, Operating Systems, performance. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
No trackbacks yet.
Case insensitive Map key – code smell
about 1 year ago - No comments
Here’s the bug that had me working today (a Sukot holiday): _myMap.put(key.toLowerCase()) … _myMap.get(key) // without lower casing the key. At first you might think of this as a common human error, but I claim that it’s no less of a code smell: Why trust yourself to always remember to lower/upper case all of the…
“Hypervisor edition” – what’s that?
about 1 year ago - 2 comments
WebSphere have announced WAS hypervisor edition. You get an OVF package with a ready to use WAS profile running on Linux. The OVF package can be deployed on VMWare ESX/ESXi and IBM’s cludeburst appliance. Websphere also say that they carried out WAS best-practice tuning for the OS. Not sure how mattering this tuning is considering…
IBM’s PLDE seminar 2010 – Review
about 1 year ago - No comments
I spent today at the IBM Programming Languages and Development Environments Seminar 2010, that took place at the beautiful Haifa Research lab mount Carmel campus. Things worth mentioning: Gilad Bracha, father of Java Generics and auto-boxing, spent 60 minutes repenting Sun’s Java 1.0 early design mistakes, such as allowing primitives and static members into the…
ConcurrentHashMap fat memory footprint
about 1 year ago - No comments
While running product sizing tests, we’ve found that an over enthusiastic usage of ConcurrentHashMap (CHM) had evaporated a good ~170MB of much needed heap space (we ran with a 1.5GB heap). As it turns out, a empty CHM weighs around 1700B. Yes, I’m talking about a map with no entries at all, just the plumbing!…
Concurrent Modification Exception
about 1 year ago - No comments
I ran into a ConcurrentModificationException (CME) during stress testing. What does CME actually mean? It means that you’ve modified (add, remove, update) your Collection while you’ve been iterating over it (usually in a multi-threaded fashion, but it can occur in a single thread that modifies while iterating). A few more things to note about CME:…
NAT in VMWare vSphere/ESX – In a nut shell
about 2 years ago - No comments
This post is about NATing an ESX VM, but first, why do I need NAT: The SIP protocol is not NAT oblivious. To traverse NAT our application has to replace the DNS in the SIP message contact header to the external FQDN that the message receiver will be sending responses to (A NAT with static…
Myth busting – String.intern() object allocations are never garbage collected
about 2 years ago - 7 comments
Java is becoming quite old (version 1.0 came out in 1996 if I’m not mistaken). When something turns old, legends, myths, and other perceived truths are quick to form around it (just imagine an old Gothic mansion with its stack of scare tales). Most of the accumulated knowledge is beneficial and helpful, but some of…
Extanding your troubleshooting facilities – Always on verbose GC
about 2 years ago - 4 comments
Getting it right the first time What happens when customers are experiencing problems with you application in production? The customer would send you the various logs artifacts and, ideally, you should be able to diagnose the problem and provide a resolution. If you find yourself sending the customer back and forth in an effort to…
A hand made freeware windows firewall
about 2 years ago - No comments
I have two windows servers that shouldn’t talk to each other. How do I make sure they don’t? Right, why not use some firewall? well, because I can’t just install any software on these servers, company regulations, and windows’ built-in firewall suck big time (only inbound, have to configure ALL exceptions). On Linux this is…
My first question at Stackoverflow.com
about 2 years ago - No comments
Could stackoverflow.com, or any other programming Q&A service, be the alternative for a serious think process, in which you just put in your question and immediately granted with the perfect answer? Hopefully it is. To test that I’ve submitted the following “how to regulate the amount of logging printouts” question. Let’s wait, pray, and see…
Via e-mail
about 2 years ago
Hi,
You can also tell Avi that he shouldn’t be surprised, it’s obviously the same case in C++. This is how preemptive OS’s do sleep.
Lol.
Lior
about 2 years ago
Right, now I know that it’s not a Java thing. Windows and standard Linux systems, were just not created with this level of precision in mind.