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

Valentin Kovalenko valentin.male.kovalenko at gmail.com
Fri Aug 24 13:38:42 EDT 2018

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
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).

[image: LinkedIn] <https://www.linkedin.com/in/stIncMale>   [image: GitHub]
<https://github.com/stIncMale>   [image: YouTube]

On Fri, 24 Aug 2018 at 10:02, <concurrency-interest-request at cs.oswego.edu>

> Send Concurrency-interest mailing list submissions to
>         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
> You can reach the person managing the list at
>         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>
> To: 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>
> 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
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
> ------------------------------
> End of Concurrency-interest Digest, Vol 162, Issue 12
> *****************************************************
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20180824/6acb2b0e/attachment.html>

More information about the Concurrency-interest mailing list