本文整理匯總了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";
}
示例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();
}
}
示例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
}
}
}
示例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);
}
示例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);
}
}
示例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);
}
示例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();
}
示例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();
}
示例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;
}