当前位置: 首页>>代码示例>>Java>>正文


Java TimeUnit.sleep方法代码示例

本文整理汇总了Java中java.util.concurrent.TimeUnit.sleep方法的典型用法代码示例。如果您正苦于以下问题:Java TimeUnit.sleep方法的具体用法?Java TimeUnit.sleep怎么用?Java TimeUnit.sleep使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.util.concurrent.TimeUnit的用法示例。


在下文中一共展示了TimeUnit.sleep方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: waitForEvent

import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
/**
 * 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());
}
 
开发者ID:syndesisio,项目名称:syndesis-qe,代码行数:29,代码来源:TestUtils.java

示例2: sleep

import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
protected void sleep(long t, TimeUnit tu) {
    try {
        tu.sleep(t);
    } catch (InterruptedException e) {
        LOG.info("[control-event] 线程sleep:" + t + " " + tu.name() + "中被中断!");
    }
}
 
开发者ID:BriData,项目名称:DBus,代码行数:8,代码来源:AbstractEvent.java

示例3: sleep

import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
private void sleep(long t, TimeUnit tu) {
    try {
        tu.sleep(t);
    } catch (InterruptedException e) {
        LoggerFactory.getLogger().error("[kafka-consumer-container] 线程sleep:" + t + " " + tu.name() + "中被中断!");
    }
}
 
开发者ID:BriData,项目名称:DBus,代码行数:8,代码来源:KafkaConsumerContainer.java

示例4: waitFor

import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
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);
    }
}
 
开发者ID:streamingpool,项目名称:streamingpool-core,代码行数:9,代码来源:UncheckedWaits.java

示例5: sleep

import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
public static void sleep(long timeout, @NotNull TimeUnit timeUnit) {
    try {
        timeUnit.sleep(timeout);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
 
开发者ID:tuuzed,项目名称:microhttpd,代码行数:8,代码来源:SleepUtils.java

示例6: waitForSuccess

import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
/**
 * 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;
}
 
开发者ID:bakdata,项目名称:ignite-hbase,代码行数:22,代码来源:StatusProvider.java

示例7: delayCommand

import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
/**
 * 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) {}
}
 
开发者ID:braineering,项目名称:ares,代码行数:14,代码来源:CoreController.java

示例8: wait

import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
/**
 * 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 */}
}
 
开发者ID:braineering,项目名称:ares,代码行数:12,代码来源:CoreController.java

示例9: sleepFor

import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
@Override
public void sleepFor(long time, TimeUnit unit) throws InterruptedException {
    unit.sleep(time);
}
 
开发者ID:YanXs,项目名称:nighthawk,代码行数:5,代码来源:Sleeper.java

示例10: sleepFor

import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
@Override
public void sleepFor(long time, TimeUnit unit) throws InterruptedException {
  unit.sleep(time);
}
 
开发者ID:Nordstrom,项目名称:xrpc,代码行数:5,代码来源:RetryLoop.java

示例11: waitForDiscovery

import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
/**
 * 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;
}
 
开发者ID:aguther,项目名称:dds-examples,代码行数:62,代码来源:RoutingServiceCommandInterface.java

示例12: sleep

import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
public static void sleep(TimeUnit timeUnit, long timeout)
		throws InterruptedException {
	timeUnit.sleep(timeout);
}
 
开发者ID:ahmed-ebaid,项目名称:invest-stash-rest,代码行数:5,代码来源:UsersServiceHelper.java


注:本文中的java.util.concurrent.TimeUnit.sleep方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。