[concurrency-interest] Sleep time consistency compared to System.currentTimeMillis

Nathan and Ila Reynolds nathanila at gmail.com
Sat Aug 25 09:37:51 EDT 2018


I do not have an answer for you.  However, I do not think Java is 
completely in control.  I recommend you look at the source code for 
Thread.sleep() including the JVM code.  This will show you what system 
APIs are called.  You can then look at the documentation for the system 
APIs.

Here is some anecdotal information.  We recently started using Netdata 
to track resource usage.  One metric that Netdata tracks is idle 
jitter.  Here's the Netdata description.


Idle jitter is calculated by netdata. A thread is spawned that requests 
to sleep for a few microseconds. When the system wakes it up, it 
measures how many microseconds have passed. The difference between the 
requested and the actual duration of the sleep, is the idle jitter. This 
number is useful in real-time environments, where CPU jitter can affect 
the quality of the service (like VoIP media gateways).

I have not tuned the machine to minimize idle jitter.  I am seeing an 
idle jitter of up to 10 ms!  This suggests to me that Thread.sleep(1) 
could return up to 10 ms later.  For my application, I do not think this 
is a big deal.  I am not doing any time sensitive operations.  I pasted 
in the idle jitter graph.  I hope it makes it through the mail.

-Nathan

On 8/24/2018 11:38 AM, Valentin Kovalenko via Concurrency-interest wrote:
> Hi Thorsten,
>
> Why do you think that Thread.sleep can wake up earlier than was 
> requested? There is no mention of this in the spec. Thread.sleep will 
> never wake up earlier, but besides that, you have no guarantees except 
> "the best effort to not exceed the specified time interval by much".
>
> >code that also uses System.currentTimeMillies can break because 
> wakeups happen to early
> Thread.sleed definitely can't use System.currentTimeMillies, because 
> currentTimeMillies is not monotonic (it's just a system time, which 
> can easily go backwards). If you are trying to reliably measure an 
> interval of time, use System.nanoTime because it is monotonic (this 
> method is specifically there to provide a tool for measuring time 
> intervals).
>
> Regards,
> Valentin
> LinkedIn <https://www.linkedin.com/in/stIncMale>GitHub 
> <https://github.com/stIncMale>YouTube 
> <https://www.youtube.com/user/stIncMale>
>
>
> On Fri, 24 Aug 2018 at 10:02, 
> <concurrency-interest-request at cs.oswego.edu 
> <mailto:concurrency-interest-request at cs.oswego.edu>> wrote:
>
>     Send Concurrency-interest mailing list submissions to
>     concurrency-interest at cs.oswego.edu
>     <mailto:concurrency-interest at cs.oswego.edu>
>
>     To subscribe or unsubscribe via the World Wide Web, visit
>     http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>     or, via email, send a message with subject or body 'help' to
>     concurrency-interest-request at cs.oswego.edu
>     <mailto:concurrency-interest-request at cs.oswego.edu>
>
>     You can reach the person managing the list at
>     concurrency-interest-owner at cs.oswego.edu
>     <mailto:concurrency-interest-owner at cs.oswego.edu>
>
>     When replying, please edit your Subject line so it is more specific
>     than "Re: Contents of Concurrency-interest digest..."
>
>
>     Today's Topics:
>
>        1. Sleep time consistency compared to System.currentTimeMillis
>           (Thorsten Goetzke)
>
>
>     ----------------------------------------------------------------------
>
>     Message: 1
>     Date: Fri, 24 Aug 2018 11:10:20 +0200
>     From: Thorsten Goetzke <tg at freigmbh.de <mailto:tg at freigmbh.de>>
>     To: concurrency-interest at cs.oswego.edu
>     <mailto:concurrency-interest at cs.oswego.edu>
>     Subject: [concurrency-interest] Sleep time consistency compared to
>             System.currentTimeMillis
>     Message-ID: <ffbde00d-268c-7ff5-ab1a-cd2499cadd36 at freigmbh.de
>     <mailto:ffbde00d-268c-7ff5-ab1a-cd2499cadd36 at freigmbh.de>>
>     Content-Type: text/plain; charset=utf-8; format=flowed
>
>     Hello,
>
>     There are multiple Classes that let you wait or sleep for a specified
>     amount of time. My current understanding is:
>     Thread.sleep and Object.wait can wake up earlier than specified so
>     code
>     that also uses System.currentTimeMillies can break because wakeups
>     happen to early.
>     On the other side code that uses higher level Abstraction such as
>     ScheduledExecutorService or CountDownLatch.await will always at least
>     wait for the specified time and it will be consistent with
>     System.currentTimeMillies. Is that actually correct and guaranteed
>     or is
>     is just implementation dependent?
>
>     Best regards,
>     Thorsten Goetzke
>
>
>     ------------------------------
>
>     Subject: Digest Footer
>
>     _______________________________________________
>     Concurrency-interest mailing list
>     Concurrency-interest at cs.oswego.edu
>     <mailto:Concurrency-interest at cs.oswego.edu>
>     http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>
>
>     ------------------------------
>
>     End of Concurrency-interest Digest, Vol 162, Issue 12
>     *****************************************************
>
>
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20180825/b31619ee/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: halphagbncmpejej.png
Type: image/png
Size: 78826 bytes
Desc: not available
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20180825/b31619ee/attachment-0001.png>


More information about the Concurrency-interest mailing list