You are not logged in.
Pages: 1
I have an application that I want to guarantee that when I call Java's Thread.sleep(n) the thread will sleep for precisely that amount of time, or close to it at least. What we recently encountered though was that if the "wall clock" time is changed in one thread while a sleep is occurring in another thread, this clock change will impact when the sleeping thread will wake up.
In our case, we had a simple Thread.sleep(1000), and during that one second period another thread set the clock back two minutes. The sleeping thread ended up sleeping roughly two minutes and a second instead of just a second. Apparently this is a "feature" of Thread.sleep(), and I can see in some cases (e.g. a task scheduler based on wall clock times) where this is exactly the behavior you'd want. However, in our case, we want the sleep function to sleep by the amount we specify, regardless of changes to the wall clock time. Is there a way to do this in Java?
Offline
I should note that our development platform is FreeBSD 7.0 with JDK 1.5.
Offline
Try using nanosleep instead of sleep. No idea if that's easily done with Java, Java's
sleep function should use nanosleep already actually. If it does and the timewarp
stufff still happens then it's a FreeBSD bug.
Offline
I wrote a jni wrapper to call the C nanosleep function. It seems to work as we need. Thanks for the pointer.
Offline
Pages: 1