Esempio n. 1
1
 public static void waitFor(Object lock, BooleanSupplier supplier) throws InterruptedException {
   synchronized (lock) {
     while (!supplier.getAsBoolean()) {
       lock.wait();
     }
   }
 }
  /*
   * BooleanSupplier. Supplier of boolean-valued results.
   * IntSupplier. Supplier of int-valued results.
   * LongSupplier. Supplier of long-valued results.
   * DoubleSupplier. Supplier of double-valued results.
   */
  public static void main(String[] args) {
    BooleanSupplier booleanSupplier = () -> Boolean.TRUE;
    IntSupplier intSupplier = () -> new Integer(0);
    LongSupplier longSupplier = () -> new Long(0);
    DoubleSupplier doubleSupplier = () -> new Double(0);

    System.out.println(booleanSupplier.getAsBoolean());
    System.out.println(intSupplier.getAsInt());
    System.out.println(longSupplier.getAsLong());
    System.out.println(doubleSupplier.getAsDouble());
  }
Esempio n. 3
0
 private void doEventLoopWaitUntil(long start, BooleanSupplier supplier, Runnable runner) {
   long now = System.currentTimeMillis();
   if (now - start > 10000) {
     fail("Timedout in waiting until");
   } else {
     if (supplier.getAsBoolean()) {
       runner.run();
     } else {
       vertx.setTimer(1, tid -> doEventLoopWaitUntil(start, supplier, runner));
     }
   }
 }
  private long processTimersUntil(final BooleanSupplier condition) {
    final long start = wheel.clock().nanoTime();

    while (!condition.getAsBoolean()) {
      if (wheel.computeDelayInMs() > 0) {
        currentTime += TimeUnit.MICROSECONDS.toNanos(Configuration.CONDUCTOR_TICK_DURATION_US);
      }

      wheel.expireTimers();
    }

    return wheel.clock().nanoTime() - start;
  }
Esempio n. 5
0
    @Override
    public void run() {

      while (continueRunning.getAsBoolean()) {

        Platform.runLater(this::scrollY);

        try {
          sleep(SCROLL_THREAD_SLEEP_INTERVAL);
        } catch (InterruptedException e) {
          // Do nothing
        }
      }
    }
Esempio n. 6
0
 public static boolean waitFor(Object lock, BooleanSupplier supplier, long millis)
     throws InterruptedException {
   if (millis < 0) {
     throw new IllegalArgumentException();
   } else if (millis == 0) {
     waitFor(lock, supplier);
     return true;
   } else {
     long t1 = System.currentTimeMillis(), t2;
     long remaining = millis;
     boolean b;
     synchronized (lock) {
       while (!(b = supplier.getAsBoolean()) && remaining > 0) {
         lock.wait(millis);
         t2 = System.currentTimeMillis();
         remaining -= (t2 - t1);
         t1 = t2;
       }
     }
     return b;
   }
 }
  /**
   * Blocking method that waits until {@code conditionSupplier} returns true, or if it does not
   * before the specified timeout, throws an {@link AssertionError} with the specified error message
   * supplier.
   *
   * @param timeout the timeout duration
   * @param errorMessageSupplier the error message supplier
   * @param conditionSupplier condition to break out of the wait loop
   */
  public static void await(
      Duration timeout, Supplier<String> errorMessageSupplier, BooleanSupplier conditionSupplier) {

    Objects.requireNonNull(errorMessageSupplier);
    Objects.requireNonNull(conditionSupplier);
    Objects.requireNonNull(timeout);

    long timeoutNs = timeout.toNanos();
    long startTime = System.nanoTime();
    do {
      if (conditionSupplier.getAsBoolean()) {
        return;
      }
      try {
        Thread.sleep(100);
      } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new RuntimeException(e);
      }
    } while (System.nanoTime() - startTime < timeoutNs);
    throw new AssertionError(errorMessageSupplier.get());
  }
Esempio n. 8
0
 private void waitFor(BooleanSupplier b) {
   for (int i = 1; i <= 40; i++) if (!b.getAsBoolean()) Jvm.pause(i * i);
 }
Esempio n. 9
0
 /**
  * When the supplied condition is {@code true}, call the supplied function with this
  * Configurator.
  *
  * @param condition the function that determines whether the supplied function should be called;
  *     may not be null
  * @param configure the function that will perform additional configuration
  * @return this configurator so that methods can be chained together; never null
  */
 public Configurator when(BooleanSupplier condition, Consumer<Configurator> configure) {
   if (condition != null && configure != null && condition.getAsBoolean()) {
     configure.accept(this);
   }
   return this;
 }
Esempio n. 10
0
 /**
  * When the supplied condition is {@code true}, call the supplied function with this
  * Configurator.
  *
  * @param condition the function that determines whether the supplied function should be called;
  *     may not be null
  * @param configure the function that will perform additional configuration
  * @return this configurator so that methods can be chained together; never null
  */
 public Configurator when(BooleanSupplier condition, Runnable configure) {
   if (condition != null && configure != null && condition.getAsBoolean()) {
     configure.run();
   }
   return this;
 }
Esempio n. 11
0
 public static <T> void assertThat(BooleanSupplier assertion, Supplier<T> expr) {
   if (enableAssert && !assertion.getAsBoolean()) {
     System.out.println("error 2");
     throw new AssertionError(expr.get());
   }
 }
Esempio n. 12
0
 public static void assertThat(BooleanSupplier assertion) {
   if (enableAssert && !assertion.getAsBoolean()) {
     throw new AssertionError();
   }
 }