Java Code Examples for java.util.concurrent.TimeUnit.sleep()

The following are Jave code examples for showing how to use sleep() of the java.util.concurrent.TimeUnit class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: syndesis-qe   File: TestUtils.java   View Source Code Vote up 6 votes
/**
 * Waits until a predicate is true or timeout exceeds.
 *
 * @param predicate predicate
 * @param supplier supplier of values to test by predicate
 * @param unit TimeUnit for timeout
 * @param timeout how long to wait for event
 * @param sleepUnit TimeUnit of sleep interval between tests
 * @param sleepTime how long to wait between individual tests (in miliseconds)
 * @param <T> Type of tested value by a predicate
 * @return True if predicate become true within a timeout, otherwise returns false.
 */
public static <T> boolean waitForEvent(Predicate<T> predicate, Supplier<T> supplier, TimeUnit unit, long timeout, TimeUnit sleepUnit, long sleepTime) {

	final long start = System.currentTimeMillis();
	long elapsed = 0;
	while (!predicate.test(supplier.get()) && unit.toMillis(timeout) >= elapsed) {
		try {
			sleepUnit.sleep(sleepTime);
		} catch (InterruptedException e) {
			log.debug("Interupted while sleeping", e);
		} finally {
			elapsed = System.currentTimeMillis() - start;
		}
	}

	return predicate.test(supplier.get());
}
 
Example 2
Project: DBus   File: AbstractEvent.java   View Source Code Vote up 5 votes
protected void sleep(long t, TimeUnit tu) {
    try {
        tu.sleep(t);
    } catch (InterruptedException e) {
        LOG.info("[control-event] 线程sleep:" + t + " " + tu.name() + "中被中断!");
    }
}
 
Example 3
Project: DBus   File: KafkaConsumerContainer.java   View Source Code Vote up 5 votes
private void sleep(long t, TimeUnit tu) {
    try {
        tu.sleep(t);
    } catch (InterruptedException e) {
        LoggerFactory.getLogger().error("[kafka-consumer-container] 线程sleep:" + t + " " + tu.name() + "中被中断!");
    }
}
 
Example 4
Project: DBus   File: EventContainer.java   View Source Code Vote up 5 votes
private void sleep(long t, TimeUnit tu) {
    try {
        tu.sleep(t);
    } catch (InterruptedException e) {
        LoggerFactory.getLogger().error("[kafka-consumer-container] 线程sleep:" + t + " " + tu.name() + "中被中断!");
    }
}
 
Example 5
Project: streamingpool-core   File: UncheckedWaits.java   View Source Code Vote up 5 votes
public static void waitFor(long duration, TimeUnit timeUnit) {
    Objects.requireNonNull(timeUnit, "timeUnit must not be null");
    try {
        timeUnit.sleep(duration);
    } catch (InterruptedException e) {
        throw new RuntimeException("Error while waiting.", e);
    }
}
 
Example 6
Project: microhttpd   File: SleepUtils.java   View Source Code Vote up 5 votes
public static void sleep(long timeout, @NotNull TimeUnit timeUnit) {
    try {
        timeUnit.sleep(timeout);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
 
Example 7
Project: ignite-hbase   File: StatusProvider.java   View Source Code Vote up 5 votes
/**
 * Poll status periodically until true or timeout reached.
 *
 * @param timeout time to wait for status
 * @param timeUnit time unit
 * @return true if successful, false if timeout reached before success
 * @throws IOException if an error polling status occurs {@link #success()}
 * @throws InterruptedException if thread is interrupted while waiting
 */
default boolean waitForSuccess(long timeout, TimeUnit timeUnit)
    throws IOException, InterruptedException {
  long start = System.currentTimeMillis();
  while (!success()) {
    long delta = System.currentTimeMillis() - start;
    if (delta > timeout) {
      return false;
    }
    timeUnit.sleep(5L);
  }
  return true;
}
 
Example 8
Project: ares   File: CoreController.java   View Source Code Vote up 5 votes
/**
 * Waits {@code delay} before the execution of the command with {@code scope}.
 * @param delay the time to wait.
 * @param scope the command to wait for.
 */
private static void delayCommand(Duration delay, CommandScope scope) {
  final long amount = delay.getAmount();
  final TimeUnit unit = delay.getUnit();
  LOGGER.info("Waiting before command {} with delay {} {}", scope, amount, unit);
  try {
    unit.sleep(amount);
  } catch (InterruptedException ignored) {}
}
 
Example 9
Project: ares   File: CoreController.java   View Source Code Vote up 5 votes
/**
 * Make the bot sleep for the specified duration.
 * @param timeout the sleeping period.
 */
private static void wait(Duration timeout) {
  final long amount = timeout.getAmount();
  final TimeUnit unit = timeout.getUnit();
  try {
    unit.sleep(amount);
  } catch (InterruptedException ignored) { /* ignored */}
}
 
Example 10
Project: nighthawk   File: Sleeper.java   View Source Code Vote up 4 votes
@Override
public void sleepFor(long time, TimeUnit unit) throws InterruptedException {
    unit.sleep(time);
}
 
Example 11
Project: xrpc   File: RetryLoop.java   View Source Code Vote up 4 votes
@Override
public void sleepFor(long time, TimeUnit unit) throws InterruptedException {
  unit.sleep(time);
}
 
Example 12
Project: dds-examples   File: RoutingServiceCommandInterface.java   View Source Code Vote up 4 votes
/**
 * Waits for a specific routing service instance to be discovered.
 *
 * @param targetRouter target routing service
 * @param timeOut timeout
 * @param timeOutUnit time unit of timeout
 * @param sleepTime time to sleep between checks
 * @param sleepTimeUnit time unit of time to sleep
 * @return true if target routing service was discovered, false if not within timeout
 */
public boolean waitForDiscovery(
    final String targetRouter,
    final long timeOut,
    final TimeUnit timeOutUnit,
    final long sleepTime,
    final TimeUnit sleepTimeUnit
) {
  // create participant name for target router according RTI conventions
  String participantNameTargetRouter = String.format("RTI Routing Service: %s", targetRouter);

  try {
    // variables to store the data
    InstanceHandleSeq instanceHandles = new InstanceHandleSeq();
    ParticipantBuiltinTopicData participantData = new ParticipantBuiltinTopicData();

    // store start time
    long startTime = System.currentTimeMillis();
    // determine end time
    long endTime = startTime + timeOutUnit.toMillis(timeOut);

    while (System.currentTimeMillis() < endTime) {
      // get matched subscriptions
      requester.getRequestDataWriter().get_matched_subscriptions(instanceHandles);

      // iterate over instance handles
      for (Object participantHandle : instanceHandles) {
        // get participant data of subscription
        requester.getRequestDataWriter().get_matched_subscription_participant_data(
            participantData,
            (InstanceHandle_t) participantHandle
        );

        // check if related participant is from routing service
        if (participantData.service.kind == ServiceQosPolicyKind.ROUTING_SERVICE_QOS
            && participantData.participant_name.name.equals(participantNameTargetRouter)) {
          // we discovered the target routing service
          return true;
        }
      }

      // sleep for some time
      sleepTimeUnit.sleep(sleepTime);
    }

  } catch (InterruptedException e) {
    Thread.currentThread().interrupt();
  }

  // we did not discover the target routing service
  return false;
}
 
Example 13
Project: invest-stash-rest   File: UsersServiceHelper.java   View Source Code Vote up 4 votes
public static void sleep(TimeUnit timeUnit, long timeout)
		throws InterruptedException {
	timeUnit.sleep(timeout);
}