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

孤街醉人 提交于 2019-12-05 07:00:25

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.

The OS only reacts at interrupts and therefore handles sleep expiries at the time of an interrupt. It is correct the the interrup frequency can be increased by means of timeBeginPeriod. The difficulty is that the expiry of the Sleep() function requires two conditions to be met:

  1. An Interrupt has to occur
  2. dwMilliseconds has to expire.

Condition 2 is the problem here. The dwMilliseconds will be compared to the expired system time at interrupts. The system time will cause the Sleep() function to expire in filetime-format increments, in other words when n times the system-time increment becomes larger than dwMilliseconds. Thus one may not be able to ever get 1ms sleep delays. This heavely depends on the systems hard- and software and configuration (system-time increment/granularity).

A closer look with some examples can be found here

To answer the question: Thread.sleep(1000) sleeps for >= 1000 milliseconds is always TRUE! Edit: When executed right after a Thread.sleep(1)

Edit: However Thread.sleep(1) sleeps for >= 1 milliseconds may not always be TRUE

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!