Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does Thread.sleep(1000) sleeps less than 1000 milliseconds?

In this interesting article about falsehoods programmers believe about time, one of them is

Thread.sleep(1000) sleeps for >= 1000 milliseconds.

When isn't this true?

like image 424
ripper234 Avatar asked Jul 07 '12 15:07

ripper234


1 Answers

According to this (Implementation of the sleep by windows operating system, which is what Thread.sleep will call underneath): If dwMilliseconds is less than the resolution of the system clock, the thread may sleep for less than the specified length of time. If dwMilliseconds is greater than one tick but less than two, the wait can be anywhere between one and two ticks, and so on. To increase the accuracy of the sleep interval, call the timeGetDevCaps function to determine the supported minimum timer resolution and the timeBeginPeriod function to set the timer resolution to its minimum.

like image 121
user1494736 Avatar answered Oct 14 '22 16:10

user1494736