Conversion of nanoseconds to milliseconds and nanoseconds < 999999 in Java

87,036

Solution 1

Just take the divmod of it with 1000000.

Solution 2

Why not use the built in Java methods. The TimeUnit is part of the concurrent package so built exactly for you needs

  long durationInMs = TimeUnit.MILLISECONDS.convert(delayNS, TimeUnit.NANOSECONDS);

Solution 3

For an ever shorter conversion using java.util.concurrent.TimeUnit, equivalent to what Shawn wrote above, you can use:

    long durationInMs = TimeUnit.NANOSECONDS.toMillis(delayNS);

Solution 4

The TimeUnit.timedWait(Object obj, long timeout) is what you should use. It does this calculation for you and then calls wait on the object.

https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/TimeUnit.html#timedWait(java.lang.Object,%20long)

The implementation from Java 8 (to see that it is computing the ms and ns based off nanoseconds):

public void timedWait(Object obj, long timeout)
        throws InterruptedException {
    if (timeout > 0) {
        long ms = toMillis(timeout);
        int ns = excessNanos(timeout, ms);
        obj.wait(ms, ns);
    }
}

So you would use

TimeUnit.NANOSECONDS.timedWait(EVTLOCK, delayNS);

There are also sleep and timedJoin methods that are similar.

Share:
87,036
Chris Dennett
Author by

Chris Dennett

Software developer and games programmer :)

Updated on July 01, 2020

Comments

  • Chris Dennett
    Chris Dennett almost 4 years

    I'm wondering what the most accurate way of converting a big nanoseconds value is to milliseconds and nanoseconds, with an upper limit on the nanoseconds of 999999. The goal is to combine the nanoseconds and milliseconds values to ensure the maximum resolution possible with the limit given. This is for comparability with the sleep / wait methods and some other external library that gives out large nanosecond values.

    Edit: my code looks like the following now:

    while (hasNS3Events()) {                                
        long delayNS = getNS3EventTSDelay();
        long delayMS = 0;
        if (delayNS <= 0) runOneNS3Event();
        else {
            try {
                if (delayNS > 999999) {
                    delayMS = delayNS / 1000000;
                    delayNS = delayNS % 1000000;
                }
    
                EVTLOCK.wait(delayMS, (int)delayNS);
            } catch (InterruptedException e) {
    
            }
        }
    }
    

    Cheers, Chris