本文整理汇总了Java中java.time.Duration.ofMillis方法的典型用法代码示例。如果您正苦于以下问题:Java Duration.ofMillis方法的具体用法?Java Duration.ofMillis怎么用?Java Duration.ofMillis使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.time.Duration
的用法示例。
在下文中一共展示了Duration.ofMillis方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: formatAsElapsed
import java.time.Duration; //导入方法依赖的package包/类
public static String formatAsElapsed() {
Duration elapsed = Duration.ofMillis(ManagementFactory.getRuntimeMXBean().getUptime());
long days = elapsed.toDays();
elapsed = elapsed.minusDays(days);
long hours = elapsed.toHours();
elapsed = elapsed.minusHours(hours);
long minutes = elapsed.toMinutes();
elapsed = elapsed.minusMinutes(minutes);
long seconds = elapsed.getSeconds();
elapsed = elapsed.minusSeconds(seconds);
long millis = elapsed.toMillis();
return String.format(TIME_FORMAT,
days,
hours,
minutes,
seconds,
millis);
}
示例2: testCustomDelayForReturnValueRetry
import java.time.Duration; //导入方法依赖的package包/类
@Test public void testCustomDelayForReturnValueRetry() throws Exception {
TestDelay<String> delay = new TestDelay<String>() {
@Override public Duration duration() {
return Duration.ofMillis(1);
}
};
Retryer.ForReturnValue<String> forReturnValue =
retryer.ifReturns(s -> s.startsWith("bad"), asList(delay).stream());
when(action.run()).thenReturn("bad").thenReturn("fixed");
CompletionStage<String> stage = forReturnValue.retry(action::run, executor);
elapse(Duration.ofMillis(1));
assertCompleted(stage).isEqualTo("fixed");
verify(action, times(2)).run();
assertThat(delay.before).isEqualTo("bad");
assertThat(delay.after).isEqualTo("bad");
}
示例3: AngerRenderer
import java.time.Duration; //导入方法依赖的package包/类
public AngerRenderer(Loop maxLoopCount) {
super(maxLoopCount);
emotionImage = ImageResources.emotionAnger();
ArrayList<KeyFrameAnimation.KeyFrame> animX = new ArrayList<>();
ArrayList<KeyFrameAnimation.KeyFrame> animY = new ArrayList<>();
Instant now = Instant.now();
double r = 1f / pos.length;
for (int i = 1; i < pos.length; i++) {
LinearAnimation x = new LinearAnimation(now, Duration.ZERO);
x.setFromValue(ImageUtil.defaultScale(pos[i - 1][0]));
x.setToValue(ImageUtil.defaultScale(pos[i][0]));
animX.add(new KeyFrameAnimation.KeyFrame(r * (i - 1), r * i, x));
LinearAnimation y = new LinearAnimation(now, Duration.ZERO);
y.setFromValue(ImageUtil.defaultScale(pos[i][1]));
y.setToValue(ImageUtil.defaultScale(pos[i - 1][1]));
animY.add(new KeyFrameAnimation.KeyFrame(r * (i - 1), r * i, y));
}
moveX = new KeyFrameAnimation(now, Duration.ofMillis(2000), animX);
moveY = new KeyFrameAnimation(now, Duration.ofMillis(2000), animY);
fade.setFromValue(1);
fade.setToValue(0);
}
示例4: eventually
import java.time.Duration; //导入方法依赖的package包/类
private void eventually(Runnable work) throws Exception {
Duration patience = Duration.ofSeconds(1);
Duration interval = Duration.ofMillis(15);
int remaining = (int) (patience.toMillis() / interval.toMillis());
while (true) {
try {
work.run();
return;
} catch (Exception | AssertionError e) {
if (remaining-- == 0) {
throw e;
}
Thread.sleep(interval.toMillis());
}
}
}
示例5: should_wait_notasktimeout_when_no_task_found
import java.time.Duration; //导入方法依赖的package包/类
@Test
public void should_wait_notasktimeout_when_no_task_found() throws Exception {
Duration betweenTaskTimeout = Duration.ofHours(1L);
Duration noTaskTimeout = Duration.ofMillis(5L);
QueueConsumer queueConsumer = mock(QueueConsumer.class);
TaskPicker taskPicker = mock(TaskPicker.class);
when(taskPicker.pickTask(queueConsumer)).thenReturn(null);
TaskProcessor taskProcessor = mock(TaskProcessor.class);
when(queueConsumer.getQueueConfig()).thenReturn(new QueueConfig(testLocation1,
QueueSettings.builder().withBetweenTaskTimeout(betweenTaskTimeout).withNoTaskTimeout(noTaskTimeout).build()));
QueueProcessingStatus status = new QueueRunnerInSeparateTransactions(taskPicker, taskProcessor).runQueue(queueConsumer);
assertThat(status, equalTo(QueueProcessingStatus.SKIPPED));
verify(taskPicker).pickTask(queueConsumer);
verifyZeroInteractions(taskProcessor);
}
示例6: getShouldRetry
import java.time.Duration; //导入方法依赖的package包/类
/**
* Returns the corresponding ShouldRetry delegate.
*
* @return The ShouldRetry delegate.
*/
@Override
public ShouldRetry getShouldRetry() {
return (int currentRetryCount,
RuntimeException lastException,
ReferenceObjectHelper<Duration> refRetryInterval) -> {
if (currentRetryCount < this.retryCount) {
Double delta = this.deltaBackoff == Duration.ZERO ? 0.0
: (Math.pow(2.0, currentRetryCount) - 1) * ThreadLocalRandom.current().nextDouble((this.deltaBackoff.getSeconds() * 0.8),
(this.deltaBackoff.getSeconds() * 1.2));
Long interval = Math.min((this.minBackoff.getSeconds() + delta.intValue()), this.maxBackoff.getSeconds());
refRetryInterval.argValue = Duration.ofMillis(interval);
return true;
}
refRetryInterval.argValue = Duration.ZERO;
return false;
};
}
示例7: getTimeRemaining
import java.time.Duration; //导入方法依赖的package包/类
/**
* Get the remaining time, formatted nicely.
* @return The remaining time.
*/
public String getTimeRemaining(){
Date now = new Date();
long remainingMillis = surveyDeadline.getTime() - now.getTime();
Duration timeRemaining = Duration.ofMillis(remainingMillis);
return DurationFormatter.formatDuration(timeRemaining);
}
示例8: convertMicrosecondsToTimeString
import java.time.Duration; //导入方法依赖的package包/类
/**
* Convert microseconds to a human readable time string.
*
* @param microseconds The amount of microseconds.
* @return The human readable string representation.
*/
public static String convertMicrosecondsToTimeString(final long microseconds) {
Duration durationMilliseconds = Duration.ofMillis(microseconds / MICROSECOND_FACTOR);
long minutes = durationMilliseconds.toMinutes();
long seconds = durationMilliseconds.minusMinutes(minutes).getSeconds();
long milliseconds = durationMilliseconds.minusMinutes(minutes).minusSeconds(seconds).toMillis();
return String.format("%dm %02ds %03dms", minutes, seconds, milliseconds);
}
示例9: testSubmissionTimeoutCancelsInFlightTasks
import java.time.Duration; //导入方法依赖的package包/类
@Test public void testSubmissionTimeoutCancelsInFlightTasks() throws InterruptedException {
assumeFalse(threading == Threading.DIRECT);
assumeTrue(mode == Mode.INTERRUPTIBLY);
maxInFlight = 2;
timeout = Duration.ofMillis(1);
assertThrows(
TimeoutException.class,
() -> parallelize(serialTasks(
() -> blockFor(1), // Will be interrupted
() -> blockFor(2), // Will be interrupted
() -> translateToString(3)))); // Times out
shutdownAndAssertInterruptedKeys().containsExactly(1, 2);
assertThat(translated).doesNotContainKey(3);
}
示例10: convert
import java.time.Duration; //导入方法依赖的package包/类
@Override
public Duration convert(String source) {
source = source.trim();
if ("0".equals(source)) {
return Duration.ZERO;
}
Matcher matcher = pattern.matcher(source);
if (!matcher.matches()) throw new IllegalArgumentException("Illegal period: " + source);
long value = Long.parseLong(matcher.group(1));
switch (matcher.group(2).toLowerCase()) {
case "ns":
case "nanos":
return Duration.ofNanos(value);
case "ms":
case "msec":
case "millis":
return Duration.ofMillis(value);
case "s":
case "sec":
return Duration.ofSeconds(value);
case "m":
case "min":
return Duration.ofMinutes(value);
case "h":
case "hour":
case "hours":
return Duration.ofHours(value);
case "d":
case "day":
case "days":
return Duration.ofDays(value);
}
throw new IllegalArgumentException("Illegal unit: " + source);
}
示例11: parseDuration
import java.time.Duration; //导入方法依赖的package包/类
public static Duration parseDuration(String text) throws DateTimeParseException {
if("oo".equals(text)) return INF_POSITIVE;
// If text looks like a plain number, try to parse it as seconds,
// but be fairly strict so we don't accidentally parse a time as
// a number.
if(text.matches("^\\s*-?[0-9]+(\\.[0-9]+)?\\s*$")) {
try {
return Duration.ofMillis((long) (1000 * Double.parseDouble(text)));
} catch(NumberFormatException ignored) {}
}
return Durations.parse(text);
}
示例12: factory_millis_long
import java.time.Duration; //导入方法依赖的package包/类
@Test(dataProvider="MillisDurationNoNanos")
public void factory_millis_long(long millis, long expectedSeconds, int expectedNanoOfSecond) {
Duration test = Duration.ofMillis(millis);
assertEquals(test.getSeconds(), expectedSeconds);
assertEquals(test.getNano(), expectedNanoOfSecond);
}
示例13: computeDelayBeforeNextRetry
import java.time.Duration; //导入方法依赖的package包/类
@Override
public Duration computeDelayBeforeNextRetry(RetryPolicyContext context) {
int ceil = calculateExponentialDelay(context.retriesAttempted(), baseDelay, maxBackoffTime, numRetries);
return Duration.ofMillis((ceil / 2) + random.nextInt((ceil / 2) + 1));
}
示例14: none
import java.time.Duration; //导入方法依赖的package包/类
static BackoffStrategy none() {
return new FixedDelayBackoffStrategy(Duration.ofMillis(1));
}
示例15: computeDelayBeforeNextRetry
import java.time.Duration; //导入方法依赖的package包/类
@Override
public Duration computeDelayBeforeNextRetry(RetryPolicyContext context) {
int ceil = calculateExponentialDelay(context.retriesAttempted(), baseDelay, maxBackoffTime, numRetries);
return Duration.ofMillis(random.nextInt(ceil));
}