开发者

Conversion of nanoseconds to milliseconds and nanoseconds < 999999 in Java

开发者 https://www.devze.com 2023-01-27 14:03 出处:网络
I\'m wondering what the most accurate way of converting a big nanoseconds value is to milliseconds and nanoseconds, with an upper limit on开发者_如何学JAVA the nanoseconds of 999999. The goal is to co

I'm wondering what the most accurate way of converting a big nanoseconds value is to milliseconds and nanoseconds, with an upper limit on开发者_如何学JAVA 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


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


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


Just take the divmod of it with 1000000.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消