我有一个while循环,我希望它在一段时间后退出。
例如:
while(condition and 10 sec has not passed){
}
我有一个while循环,我希望它在一段时间后退出。
例如:
while(condition and 10 sec has not passed){
}
long startTime = System.currentTimeMillis(); //fetch starting time
while(false||(System.currentTimeMillis()-startTime)<10000)
{
// do something
}
因此声明
(System.currentTimeMillis()-startTime)<10000
检查自循环开始以来是 10 秒还是 10,000 毫秒。
编辑
正如@Julien 指出的那样,如果您在while 循环中的代码块花费大量时间,这可能会失败。因此使用ExecutorService将是一个不错的选择。
首先我们必须实现 Runnable
class MyTask implements Runnable
{
public void run() {
// add your code here
}
}
然后我们可以像这样使用 ExecutorService,
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.invokeAll(Arrays.asList(new MyTask()), 10, TimeUnit.SECONDS); // Timeout of 10 seconds.
executor.shutdown();
就像是:
long start_time = System.currentTimeMillis();
long wait_time = 10000;
long end_time = start_time + wait_time;
while (System.currentTimeMillis() < end_time) {
//..
}
应该做的伎俩。如果您还需要其他条件,那么只需将它们添加到 while 语句中。
对于同步调用,您可以使用如下内容:
private void executeSomeSyncronLongRunningProccess() {
Duration duration = Duration.ofSeconds(5);
Predicate<String> condition = x -> x.equals("success");
Supplier<String> codeToExecute = () -> {
//Your code to execute here
return "success";
};
try {
String s = runOrTimeout(duration, condition, codeToExecute);
} catch (ExecutionException e) {
LOGGER.error("During execution is error occurred", e);
throw new RuntimeException(e);
} catch (TimeoutException e) {
LOGGER.error("Failed to complete task in {} seconds.", duration.getSeconds());
throw new RuntimeException(e);
} catch (InterruptedException e) {
LOGGER.error("Failed to complete task in {} seconds.", duration.getSeconds());
throw new RuntimeException(e);
}
}
private String runOrTimeout(Duration timeout, Predicate<String> conditionOnSuccess, Supplier<String> codeToExecute) throws InterruptedException, ExecutionException, TimeoutException {
ExecutorService executorService = Executors.newSingleThreadExecutor();
Future<String> pollQueryResults = executorService.submit(() -> {
String result = null;
boolean isRunning = true;
while (isRunning) {
result = codeToExecute.get();
if (conditionOnSuccess.test(result)) {
isRunning = false;
}
}
return result;
});
return pollQueryResults.get(timeout.getSeconds(), TimeUnit.SECONDS);
}
不要使用这个
System.currentTimeMillis()-startTime
它可能会导致主机时间更改时挂起。更好地使用这种方式:
Integer i = 0;
try {
while (condition && i++ < 100) {
Thread.sleep(100);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
(100*100 = 10 秒超时)