On Wed, 26 Apr 2023 20:22:54 GMT, Aleksey Shipilev <sh...@openjdk.org> wrote:

>> Java API has the `Thread.sleep(millis, nanos)` method exposed to users. The 
>> documentation for that method clearly says the precision and accuracy are 
>> dependent on the underlying system behavior. However, it always rounds up 
>> `nanos` to 1ms when doing the actual sleep. This means users cannot do the 
>> micro-second precision sleeps, even when the underlying platform allows it. 
>> Sub-millisecond sleeps are useful to build interesting primitives, like the 
>> rate limiters that run with >1000 RPS.
>> 
>> When faced with this, some users reach for more awkward APIs like 
>> `java.util.concurrent.locks.LockSupport.parkNanos`. The use of that API for 
>> sleeps is not in line with its intent, and while it "seems to work", it 
>> might have interesting interactions with other uses of `LockSupport`. 
>> Additionally, these "sleeps" are no longer visible to monitoring tools as 
>> "normal sleeps", e.g. as `Thread.sleep` events. Therefore, it would be 
>> prudent to improve current `Thread.sleep(millis, nanos)` for sub-millisecond 
>> granularity. 
>> 
>> Fortunately, the underlying code is almost ready for this, at least on POSIX 
>> side. I skipped Windows paths, because its timers are still no good. Note 
>> that on both Linux and MacOS timers oversleep by about 50us. I have a few 
>> ideas how to improve the accuracy for them, which would be a topic for a 
>> separate PR.
>> 
>> Additional testing:
>>   - [x] New regression test
>>   - [x] New benchmark
>>   - [x] Linux x86_64 `tier1`
>>   - [x] Linux AArch64 `tier1`
>
> Aleksey Shipilev has updated the pull request with a new target base due to a 
> merge or a rebase. The pull request now contains 26 commits:
> 
>  - Merge branch 'master' into JDK-83050920-thread-sleep-subms
>  - Merge branch 'master' into JDK-83050920-thread-sleep-subms
>  - Fix Amazon copyright
>  - Merge branch 'master' into JDK-83050920-thread-sleep-subms
>  - Drop nanos_to_nanos_bounded
>  - Handle overflows
>  - More review comments
>  - Adjust test times
>  - Windows again
>  - Windows fixes: align(...) is only for power-of-two alignments
>  - ... and 16 more: https://git.openjdk.org/jdk/compare/35e7bc21...da8f0f8c

All right, thank you all! 
I plan to integrate this some time today/tomorrow.

-------------

PR Comment: https://git.openjdk.org/jdk/pull/13225#issuecomment-1525286399

Reply via email to