• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread Sleep

 
Ranch Hand
Posts: 443
3
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ..


}
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.




 
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)


 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

reply
    Bookmark Topic Watch Topic
  • New Topic