Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread Sleep

 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ..


}
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more thing in threading nothing is guarantee..
you can use timer here..Timer
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)


 
Steve Luke
Bartender
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 443
3
C++ Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 443
3
C++ Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic