Java Code Examples for java.time.Clock#millis()

The following examples show how to use java.time.Clock#millis() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WorldDownloadState.java    From besu with Apache License 2.0 6 votes vote down vote up
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;
      });
}
 
Example 2
Source File: Main.java    From Java-Coding-Problems with MIT License 6 votes vote down vote up
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 + "'");
    }
 
Example 3
Source File: RefreshableLockManager.java    From kork with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 4
Source File: TCKClock_System.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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();
}
 
Example 5
Source File: TCKClock_System.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
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();
}
 
Example 6
Source File: AbstractPool.java    From reactor-pool with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 7
Source File: TCKClock_System.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
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();
}
 
Example 8
Source File: TCKClock_System.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
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();
}
 
Example 9
Source File: BestClock.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@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");
}
 
Example 10
Source File: TCKClock_System.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
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();
}
 
Example 11
Source File: TCKClock_System.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
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();
}
 
Example 12
Source File: Server.java    From terracotta-platform with Apache License 2.0 5 votes vote down vote up
public Server computeUpTime(Clock clock) {
  if (startTime > 0) {
    upTimeSec = (clock.millis() - startTime) / 1000;
  } else {
    upTimeSec = 0;
  }
  return this;
}
 
Example 13
Source File: FreshnessTrackerTest.java    From kafka-helmsman with MIT License 5 votes vote down vote up
/**
 * 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);
}
 
Example 14
Source File: TCKClock_System.java    From j2objc with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 15
Source File: TwoPhaseCommitSinkFunction.java    From flink with Apache License 2.0 4 votes vote down vote up
long elapsedTime(Clock clock) {
	return clock.millis() - transactionStartTime;
}
 
Example 16
Source File: Main.java    From Java-Coding-Problems with MIT License 4 votes vote down vote up
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);
    }
 
Example 17
Source File: TwoPhaseCommitSinkFunction.java    From flink with Apache License 2.0 4 votes vote down vote up
long elapsedTime(Clock clock) {
	return clock.millis() - transactionStartTime;
}
 
Example 18
Source File: Trace.java    From vespa with Apache License 2.0 4 votes vote down vote up
public static Trace createNew(int traceLevel, Clock clock) {
    return new Trace(traceLevel, new TraceNode(null, clock.millis()), clock);
}
 
Example 19
Source File: TwoPhaseCommitSinkFunction.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
long elapsedTime(Clock clock) {
	return clock.millis() - transactionStartTime;
}
 
Example 20
Source File: NodeRebooter.java    From vespa with Apache License 2.0 4 votes vote down vote up
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   
}