문제

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

도움이 되었습니까?

해결책

Just take the divmod of it with 1000000.

다른 팁

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

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);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top