本文整理汇总了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());
}
示例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() + "中被中断!");
}
}
示例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() + "中被中断!");
}
}
示例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);
}
}
示例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();
}
}
示例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;
}
示例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) {}
}
示例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 */}
}
示例9: sleepFor
import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
@Override
public void sleepFor(long time, TimeUnit unit) throws InterruptedException {
unit.sleep(time);
}
示例10: sleepFor
import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
@Override
public void sleepFor(long time, TimeUnit unit) throws InterruptedException {
unit.sleep(time);
}
示例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;
}
示例12: sleep
import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
public static void sleep(TimeUnit timeUnit, long timeout)
throws InterruptedException {
timeUnit.sleep(timeout);
}