wood burning stoves*
The moose likes Threads and Synchronization and the fly likes Thread Sleep Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Thread Sleep" Watch "Thread Sleep" New topic
Author

Thread Sleep

Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 416
    
    2

Does Thread Sleep guarantee to sleep for at least n milliseconds, we're just playing with some timers on Windows ... now we're aware of issues with the underlying resolution of the Windows timers (10-16ms) native and that sleep may come back later than asked if the JVM has other things to do but we appeared to see it come back early (uninterruped). It seems occasionally to report its slept for 486 rather than 514 (now obviously double the inaccuracy in the timing resolution (two get's_ would cover it but I wouldn't have guessed this was allowed i.e. I would have guessed they would have said at least n milliseconds but how much over OS / circumstance dependent) . Anyone any experience in this area ?

Same tests on Solaris were fine and as predicted (always slept (measured)) at least as long as requested,

Colleague's code follows ..


}


"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

sleep method will cause the thread to sleep for specified number of milliseconds but it doesnot guarantee that it will sleep for that much time specified..
Complete behavior is dependent on JVM.


SCJP6.0,My blog Ranchers from Delhi
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

One more thing in threading nothing is guarantee..
you can use timer here..Timer
Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 416
    
    2

There are some guarantees in Java threading see ... the Java Memory Model as an example .... ain't C++ you know ;-) .

Yeah Timer might tidy the code but it doesn't give us any thing else in terms of the problem, unless wait is more accurate, suspect not, may try.




Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

sleep method will cause the thread to sleep for specified number of milliseconds but it doesnot guarantee that it will sleep for that much time specified..


under which conditions this Thread.sleep may break ??(That is under which conditions the Thread.sleep may break)



Save India From Corruption - Anna Hazare.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

I wonder if the problem comes from the same inaccuracies in the clock making their appearance in the Syste.currentTimeMillis() calls (and so distorting your calculated ellapsed time)? Have you tried using the System.nanoTime() to see if that makes a difference?


Steve
Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 416
    
    2

I'll try the nano timer , tried on a fast vista box but everything is ok on that ;-) I'll retry on the XP box that gave the issues.
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Yesterday i was drunk , and going through these forums so i posted this question :

under which conditions this Thread.sleep may break ??


Thread.interrupt()
Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 416
    
    2

Using system nano gave more accurate results but in the same way still earlier wakeups, tried Timer vis sleep and this gave averages around the time required Timer slightly earlier and Thread slightly higher still exhibiting too early on XP.

Bored with this now ;-)
Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 416
    
    2

Right I'll answer my own question ;-) ... I should have read this (the bit about the infinte sleep is just mad in a good way ;-) )...


http://blogs.sun.com/dholmes/entry/inside_the_hotspot_vm_clocks

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread Sleep