[concurrency-interest] Durations in existing JDK APIs

Martin Buchholz martinrb at google.com
Thu May 31 01:03:23 EDT 2018


On Wed, May 30, 2018 at 7:43 PM, Gregg Wonderly <gergg at cox.net> wrote:

> I am not sure I understand this implementation, but isn’t
>
> >        long s = convert(duration.getSeconds(), SECONDS);
>
> needing to actually be
>
> >        long s = convert(duration.getSeconds(), NANOSECONDS);
>
> so that s+n is in a common unit of measure?
>

I haven't actually run this code yet, but from looking at the javadoc both
calls to convert give something in "this" unit, so they should agree?
You could write a test to check our understanding?  Hmmm ... alright ...
here's a passing test:


    /**
     * tests for conversion between TimeUnit and Duration
     */
    public void testDuration() throws Exception {
        ThreadLocalRandom rnd = ThreadLocalRandom.current();
        long n = rnd.nextLong();
        assertEquals(n, NANOSECONDS.convert(Duration.ofNanos(n)));
        assertEquals(n, MILLISECONDS.convert(Duration.ofMillis(n)));
        assertEquals(n, SECONDS.convert(Duration.ofSeconds(n)));
        assertEquals(n /= 60, MINUTES.convert(Duration.ofMinutes(n)));
        assertEquals(n /= 60, HOURS.convert(Duration.ofHours(n)));
        assertEquals(n /= 24, DAYS.convert(Duration.ofDays(n)));
    }
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20180530/7d0deb69/attachment-0001.html>


More information about the Concurrency-interest mailing list