下面列出了java.time.Clock#millis ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public WorldDownloadState(
final CachingTaskCollection<NodeDataRequest> pendingRequests,
final int maxRequestsWithoutProgress,
final long minMillisBeforeStalling,
final Clock clock) {
this.minMillisBeforeStalling = minMillisBeforeStalling;
this.timestampOfLastProgress = clock.millis();
this.downloadWasResumed = !pendingRequests.isEmpty();
this.pendingRequests = pendingRequests;
this.maxRequestsWithoutProgress = maxRequestsWithoutProgress;
this.clock = clock;
this.internalFuture = new CompletableFuture<>();
this.downloadFuture = new CompletableFuture<>();
this.internalFuture.whenComplete(this::cleanup);
this.downloadFuture.exceptionally(
error -> {
// Propagate cancellation back to our internal future.
if (error instanceof CancellationException) {
this.internalFuture.cancel(true);
}
return null;
});
}
public static void main(String[] args) {
Clock clock = Clock.systemUTC();
long startTimeV1 = clock.millis();
int countV1 = Strings.countStringInStringV1(STRING, SUBSTRING);
displayExecutionTime(clock.millis() - startTimeV1);
System.out.println("V1: '" + SUBSTRING + "' has occured " + countV1 + " times in '" + STRING + "'");
long startTimeV2 = clock.millis();
int countV2 = Strings.countStringInStringV2(STRING, SUBSTRING);
displayExecutionTime(clock.millis() - startTimeV2);
System.out.println("V2: '" + SUBSTRING + "' has occured " + countV2 + " times in '" + STRING + "'");
long startTimeV3 = clock.millis();
int countV3 = Strings.countStringInStringV3(STRING, SUBSTRING);
displayExecutionTime(clock.millis() - startTimeV3);
System.out.println("V3: '" + SUBSTRING + "' has occured " + countV3 + " times in '" + STRING + "'");
}
public void test_millis() {
Clock system = Clock.systemUTC();
assertEquals(system.getZone(), ZoneOffset.UTC);
for (int i = 0; i < 10000; i++) {
// assume can eventually get these within 10 milliseconds
long instant = system.millis();
long systemMillis = System.currentTimeMillis();
if (systemMillis - instant < 10) {
return; // success
}
}
fail();
}
/**
* We now have two messages, one the most recent commit, one the Log-End-Offset
* | t0 | t1 | ...
* ^ - LEO
* ^ - committed
* So the lag is the residency time of t1 in the queue, or (now) - (t1)
*/
@Test
public void testUncommittedForOneMillisecond() throws Exception {
// start at 'now'
Clock current = Clock.fixed(Instant.now(), Clock.systemDefaultZone().getZone());
// (t1) is now - 1
long ingestTimestamp = current.millis() - 1;
ConsumerRecords record = createConsumerRecordsAtTimestamps(ingestTimestamp);
Consumer kafka = mock(Consumer.class);
when(kafka.poll(Duration.ofSeconds(2))).thenReturn(record);
// (t0) is now - 2
FreshnessTracker.ConsumerOffset consumer =
new FreshnessTracker.ConsumerOffset("cluster", "group", "topic", 1, 1, false);
FreshnessMetrics metrics = new FreshnessMetrics();
FreshnessTracker work = new FreshnessTracker(consumer, kafka, metrics);
work.setClockForTesting(current);
work.run();
assertEquals("Should be behind the LEO timestamp", 1,
metrics.freshness.labels("cluster", "group", "topic", "1").get(), 0);
assertEquals(0, metrics.failed.labels("cluster", "group").get(), 0);
TopicPartition tp = new TopicPartition("topic", 1);
Collection<TopicPartition> tps = Collections.singletonList(tp);
verify(kafka).assign(tps);
verify(kafka).seek(tp, 1);
verify(kafka).poll(Duration.ofSeconds(2));
verifyNoMoreInteractions(kafka);
}
public Server computeUpTime(Clock clock) {
if (startTime > 0) {
upTimeSec = (clock.millis() - startTime) / 1000;
} else {
upTimeSec = 0;
}
return this;
}
public void test_millis() {
Clock system = Clock.systemUTC();
assertEquals(system.getZone(), ZoneOffset.UTC);
for (int i = 0; i < 10000; i++) {
// assume can eventually get these within 10 milliseconds
long instant = system.millis();
long systemMillis = System.currentTimeMillis();
if (systemMillis - instant < 10) {
return; // success
}
}
fail();
}
public void test_millis() {
Clock system = Clock.systemUTC();
assertEquals(system.getZone(), ZoneOffset.UTC);
for (int i = 0; i < 10000; i++) {
// assume can eventually get these within 10 milliseconds
long instant = system.millis();
long systemMillis = System.currentTimeMillis();
if (systemMillis - instant < 10) {
return; // success
}
}
fail();
}
@Override
public long millis() {
for (Clock clock : clocks) {
try {
return clock.millis();
} catch (DateTimeException e) {
// Ignore and attempt the next clock
Log.w(TAG, e.toString());
}
}
throw new DateTimeException(
"No clocks in " + Arrays.toString(clocks) + " were able to provide time");
}
public void test_millis() {
Clock system = Clock.systemUTC();
assertEquals(system.getZone(), ZoneOffset.UTC);
for (int i = 0; i < 10000; i++) {
// assume can eventually get these within 10 milliseconds
long instant = system.millis();
long systemMillis = System.currentTimeMillis();
if (systemMillis - instant < 10) {
return; // success
}
}
fail();
}
public void test_millis() {
Clock system = Clock.systemUTC();
assertEquals(system.getZone(), ZoneOffset.UTC);
for (int i = 0; i < 10000; i++) {
// assume can eventually get these within 10 milliseconds
long instant = system.millis();
long systemMillis = System.currentTimeMillis();
if (systemMillis - instant < 10) {
return; // success
}
}
fail();
}
/**
* Use this constructor the first time a resource is created and wrapped in a {@link PooledRef}.
* @param poolable the newly created poolable
* @param metricsRecorder the recorder to use for metrics
* @param clock the {@link Clock} to use for timestamps
*/
AbstractPooledRef(T poolable, PoolMetricsRecorder metricsRecorder, Clock clock) {
this.poolable = poolable;
this.metricsRecorder = metricsRecorder;
this.clock = clock;
this.creationTimestamp = clock.millis();
this.acquireCount = 0;
this.timeSinceRelease = -2L;
this.state = STATE_IDLE;
}
public void test_millis() {
Clock system = Clock.systemUTC();
assertEquals(system.getZone(), ZoneOffset.UTC);
for (int i = 0; i < 10000; i++) {
// assume can eventually get these within 10 milliseconds
long instant = system.millis();
long systemMillis = System.currentTimeMillis();
if (systemMillis - instant < 10) {
return; // success
}
}
fail();
}
public void test_millis() {
Clock system = Clock.systemUTC();
assertEquals(system.getZone(), ZoneOffset.UTC);
for (int i = 0; i < 10000; i++) {
// assume can eventually get these within 10 milliseconds
long instant = system.millis();
long systemMillis = System.currentTimeMillis();
if (systemMillis - instant < 10) {
return; // success
}
}
fail();
}
public HeartbeatLockRequest(
Lock lock,
AtomicInteger retriesOnFailure,
Clock clock,
Duration heartbeatDuration,
boolean reuseLockVersion) {
this.lock = new AtomicReference<>(lock);
this.retriesOnFailure = retriesOnFailure;
this.clock = clock;
this.startedAt = clock.instant();
this.heartbeatDuration = heartbeatDuration;
this.reuseLockVersion = reuseLockVersion;
this.requestId = lock.getName() + lock.getOwnerName() + clock.millis();
}
NodeRebooter(NodeRepository nodeRepository, Clock clock, FlagSource flagSource) {
super(nodeRepository, Duration.ofMinutes(25));
this.rebootIntervalInDays = Flags.REBOOT_INTERVAL_IN_DAYS.bindTo(flagSource);
this.clock = clock;
this.random = new Random(clock.millis()); // seed with clock for test determinism
}
long elapsedTime(Clock clock) {
return clock.millis() - transactionStartTime;
}
public static void main(String[] args) {
Clock clock = Clock.systemUTC();
// array of numbers
int[] numbers = new int[]{4, 5, 1, 3, 7, 4, 1, 5};
// array of Melon
Melon[] melons = new Melon[]{
new Melon("Crenshaw", 2000), new Melon("Gac", 1200), new Melon("Bitter", 2200)};
// Comparators
Comparator<Melon> byType = Comparator.comparing(Melon::getType);
Comparator<Melon> byWeight = Comparator.comparing(Melon::getWeight);
System.out.println("Check only for presence:");
System.out.println("------------------------");
System.out.println("\nSimple solution for numbers:");
long startTimeV1 = clock.millis();
boolean found1 = ArraySearch.containsElementV1(numbers.clone(), 1);
displayExecutionTime(clock.millis() - startTimeV1);
System.out.println("The element was found?: " + found1);
System.out.println("\nSolution based on Arrays.binarySearch() for numbers:");
long startTimeV2 = clock.millis();
boolean found2 = ArraySearch.containsElementV2(numbers.clone(), 1);
displayExecutionTime(clock.millis() - startTimeV2);
System.out.println("The element was found?: " + found2);
System.out.println("\nSolution based on Stream.anyMatch() for numbers:");
long startTimeV3 = clock.millis();
boolean found3 = ArraySearch.containsElementV3(numbers.clone(), 1);
displayExecutionTime(clock.millis() - startTimeV3);
System.out.println("The element was found?: " + found3);
System.out.println("\nSimple solution for Melon:");
long startTimeV4 = clock.millis();
boolean found4 = ArraySearch.containsElementObjectV1(melons.clone(), new Melon("Gac", 1200));
displayExecutionTime(clock.millis() - startTimeV4);
System.out.println("The Melon was found?: " + found4);
System.out.println("\nSolution based on Comparator for Melon:");
long startTimeV5 = clock.millis();
boolean found5 = ArraySearch.containsElementObjectV2(melons.clone(), new Melon("Gac", 1205), byType);
displayExecutionTime(clock.millis() - startTimeV5);
System.out.println("The Melon was found?: " + found5);
System.out.println("\nSolution based on binarySearch() for Melon:");
long startTimeV6 = clock.millis();
boolean found6 = ArraySearch.containsElementObjectV3(melons.clone(), new Melon("Honeydew", 1200), byWeight);
displayExecutionTime(clock.millis() - startTimeV6);
System.out.println("The Melon was found?: " + found6);
System.out.println("\nCheck only for index:");
System.out.println("---------------------");
System.out.println("\nSimple solution for numbers:");
long startTimeV7 = clock.millis();
int index1 = ArraySearch.findIndexOfElementV1(numbers.clone(), 7);
displayExecutionTime(clock.millis() - startTimeV7);
System.out.println("Found at index (-1 means not found): " + index1);
System.out.println("\nSolution based on filter() for numbers:");
long startTimeV8 = clock.millis();
int index2 = ArraySearch.findIndexOfElementV2(numbers.clone(), 7);
displayExecutionTime(clock.millis() - startTimeV8);
System.out.println("Found at index (-1 means not found): " + index2);
System.out.println("\nSimple solution for Melon:");
long startTimeV9 = clock.millis();
int index3 = ArraySearch.findIndexOfElementObjectV1(melons.clone(), new Melon("Gac", 1200));
displayExecutionTime(clock.millis() - startTimeV9);
System.out.println("Melon found at index (-1 means not found): " + index3);
System.out.println("\nSolution based on Comparator for Melon:");
long startTimeV10 = clock.millis();
int index4 = ArraySearch.findIndexOfElementObjectV2(melons.clone(), new Melon("Gac", 1205), byType);
displayExecutionTime(clock.millis() - startTimeV10);
System.out.println("Melon found at index (-1 means not found): " + index4);
System.out.println("\nSolution based on filter() and Comparator for Melon:");
long startTimeV11 = clock.millis();
int index5 = ArraySearch.findIndexOfElementObjectV3(melons.clone(), new Melon("Honeydew", 1200), byWeight);
displayExecutionTime(clock.millis() - startTimeV11);
System.out.println("Melon found at index (-1 means not found): " + index5);
}
long elapsedTime(Clock clock) {
return clock.millis() - transactionStartTime;
}
long elapsedTime(Clock clock) {
return clock.millis() - transactionStartTime;
}
public static Trace createNew(int traceLevel, Clock clock) {
return new Trace(traceLevel, new TraceNode(null, clock.millis()), clock);
}