當前位置: 首頁>>代碼示例>>Java>>正文


Java Duration.toNanos方法代碼示例

本文整理匯總了Java中java.time.Duration.toNanos方法的典型用法代碼示例。如果您正苦於以下問題:Java Duration.toNanos方法的具體用法?Java Duration.toNanos怎麽用?Java Duration.toNanos使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.time.Duration的用法示例。


在下文中一共展示了Duration.toNanos方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: format

import java.time.Duration; //導入方法依賴的package包/類
public String format(Duration object) {
    if (object.isZero()) {
        return "0";
    }
    if (Duration.ofDays(object.toDays()).equals(object)) {
        return object.toDays() + "d";
    }
    if (Duration.ofHours(object.toHours()).equals(object)) {
        return object.toHours() + "h";
    }
    if (Duration.ofMinutes(object.toMinutes()).equals(object)) {
        return object.toMinutes() + "m";
    }
    if (Duration.ofSeconds(object.getSeconds()).equals(object)) {
        return object.getSeconds() + "s";
    }
    if (Duration.ofMillis(object.toMillis()).equals(object)) {
        return object.toMillis() + "ms";
    }
    return object.toNanos() + "ns";
}
 
開發者ID:papyrusglobal,項目名稱:state-channels,代碼行數:22,代碼來源:DurationConverter.java

示例2: safeSleep

import java.time.Duration; //導入方法依賴的package包/類
public static void safeSleep(Duration duration) {
    long target = nanoTime() + duration.toNanos();
    boolean interrupted = false;
    do {
        try {
            sleep( duration.toMillis() );
        } catch ( InterruptedException e ) {
            interrupted = true;
            // retry
        }
    } while ( nanoTime() < target );

    if (interrupted) {
        Thread.currentThread().interrupt();
    }
}
 
開發者ID:agroal,項目名稱:agroal,代碼行數:17,代碼來源:AgroalTestHelper.java

示例3: test5

import java.time.Duration; //導入方法依賴的package包/類
@Test
public static void test5() {
    ProcessHandle self = ProcessHandle.current();
    Random r = new Random();
    for (int i = 0; i < 30; i++) {
        Instant end = Instant.now().plusMillis(500L);
        while (end.isBefore(Instant.now())) {
            // burn the cpu time checking the time
            long x = r.nextLong();
        }
        if (self.info().totalCpuDuration().isPresent()) {
            Duration totalCpu = self.info().totalCpuDuration().get();
            long infoTotalCputime = totalCpu.toNanos();
            long beanCputime = ProcessUtil.MXBeanCpuTime().toNanos();
            System.out.printf(" infoTotal: %12d, beanCpu: %12d, diff: %12d%n",
                    infoTotalCputime, beanCputime, beanCputime - infoTotalCputime);
        } else {
            break;  // nothing to compare; continue
        }
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:22,代碼來源:InfoTest.java

示例4: toUnit

import java.time.Duration; //導入方法依賴的package包/類
public static long toUnit(TemporalUnit unit, Duration duration) {
    switch((ChronoUnit) unit) {
        case NANOS:     return duration.toNanos();
        case MICROS:    return toMicros(duration);
        case MILLIS:    return duration.toMillis();
        case SECONDS:   return duration.getSeconds();
    }

    if(unit.getDuration().getNano() == 0) {
        return duration.getSeconds() / unit.getDuration().getSeconds();
    }

    throw new IllegalArgumentException("Unsupported sub-second unit " + unit);
}
 
開發者ID:OvercastNetwork,項目名稱:ProjectAres,代碼行數:15,代碼來源:TimeUtils.java

示例5: from

import java.time.Duration; //導入方法依賴的package包/類
public static double from(Duration duration, DurationUnit unit) throws IllegalArgumentException {
	DBUtil.notNull(duration, "The duration must not be null!");
	DBUtil.notNull(unit, "The unit must not be null!");
	switch (unit) {
		case NANOS: return duration.toNanos();
		case MILLIS: return duration.toNanos() / 1000.0 / 1000.0;
		case SECONDS: return duration.toNanos() / 1000.0 / 1000.0 / 1000.0;
		case MINUTES: return duration.toNanos() / 1000.0 / 1000.0 / 1000.0 / 60.0;
		case HOURS: return duration.toNanos() / 1000.0 / 1000.0 / 1000.0 / 60.0 / 60.0;
		case DAYS: return duration.toNanos() / 1000.0 / 1000.0 / 1000.0 / 60.0 / 60.0 / 24.0;
		default: throw new IllegalArgumentException("Unsupported duration unit: " + unit);
	}
}
 
開發者ID:Craftolution,項目名稱:CraftoDB,代碼行數:14,代碼來源:DBDurationUtil.java

示例6: Reference

import java.time.Duration; //導入方法依賴的package包/類
Reference(Object referent, Duration within, boolean forceCollection) {
    super(referent, queue);
    this.deadlineNanos = System.nanoTime() + within.toNanos();
    this.forceCollection = forceCollection;
    this.label = referent.getClass().getName() + ":" + System.identityHashCode(referent);
}
 
開發者ID:OvercastNetwork,項目名稱:ProjectAres,代碼行數:7,代碼來源:LeakDetectorImpl.java

示例7: test_toNanos_tooBig

import java.time.Duration; //導入方法依賴的package包/類
@Test(expectedExceptions=ArithmeticException.class)
public void test_toNanos_tooBig() {
    Duration test = Duration.ofSeconds(0, Long.MAX_VALUE).plusNanos(1);
    test.toNanos();
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:6,代碼來源:TCKDuration.java

示例8: test_toNanos_tooSmall

import java.time.Duration; //導入方法依賴的package包/類
@Test(expectedExceptions=ArithmeticException.class)
public void test_toNanos_tooSmall() {
    Duration test = Duration.ofSeconds(0, Long.MIN_VALUE).minusNanos(1);
    test.toNanos();
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:6,代碼來源:TCKDuration.java

示例9: checkEpsilon

import java.time.Duration; //導入方法依賴的package包/類
/**
 * Check two Durations, the second should be greater than the first or
 * within the supplied Epsilon.
 * @param d1 a Duration - presumed to be shorter
 * @param d2 a 2nd Duration - presumed to be greater (or within Epsilon)
 * @param epsilon Epsilon the amount of overlap allowed
 * @return true if d2 is greater than d1 or within epsilon, false otherwise
 */
static boolean checkEpsilon(Duration d1, Duration d2, Duration epsilon) {
    if (d1.toNanos() <= d2.toNanos()) {
        return true;
    }
    Duration diff = d1.minus(d2).abs();
    return diff.compareTo(epsilon) <= 0;
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:16,代碼來源:InfoTest.java


注:本文中的java.time.Duration.toNanos方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。