设置定时器/什么都不等
set timers / wait for nothing
我的情况是,从我的应用程序打开 waze 等待 25 秒,转到主屏幕,等待 10 秒,然后系统返回到 waze,然后我等待 15 秒然后再次进入主页并在那里等待 10 秒,
mainScreen.openWaze();
TimeWatch watch = TimeWatch.start();
double passedTimeInSeconds = 0;
System.out.println("start");
while (passedTimeInSeconds < 26.0) {
passedTimeInSeconds = watch.time(TimeUnit.SECONDS);
System.out.println("the seconds of 1 " + passedTimeInSeconds);
wazeInApp.validateWhereToField();
}
watch.reset();
passedTimeInSeconds = 0;
wazeInApp.goToHomeScreen();
while (passedTimeInSeconds < 11.0) {
passedTimeInSeconds = watch.time(TimeUnit.SECONDS);
System.out.println("the seconds of 2 " + passedTimeInSeconds);
wazeInApp.validateWhereToField();
}
watch.reset();
passedTimeInSeconds = 0;
while (passedTimeInSeconds < 16.0) {
passedTimeInSeconds = watch.time(TimeUnit.SECONDS);
System.out.println("the seconds 3 " + passedTimeInSeconds);
wazeInApp.validateWhereToField();
}
wazeInApp.goToHomeScreen();
我的控制台打印:
start
the seconds of 1 0.0
the seconds of 1 12.0
the seconds of 1 13.0
the seconds of 1 13.0
the seconds of 1 34.0
the seconds of 2 0.0
the seconds of 2 0.0
the seconds of 2 0.0
the seconds of 2 0.0
the seconds of 2 9.0
the seconds of 2 15.0
the seconds 3 0.0
the seconds 3 2.0
the seconds 3 4.0
the seconds 3 6.0
the seconds 3 9.0
the seconds 3 11.0
the seconds 3 13.0
the seconds 3 34.0
看起来很奇怪,打印的时间不对,而且超出了时间限制,而且这些是只是等待(无所事事)的正确解决方案吗?
谢谢
我相信您在这 26 秒内尝试检查某些东西,所以您不喜欢 Thread.sleep()
我尝试了以下方法并且对我有用。在这里,我将系统当前秒数加上 26(我们的预期时间),因此循环将从当前秒数开始迭代到 26 秒。
long passedTimeInSeconds = 0, earlierSecond = 0, currentSecond = 0;
long waitTillTime = Instant.now().getEpochSecond() +26;
while(passedTimeInSeconds < waitTillTime){
passedTimeInSeconds = Instant.now().getEpochSecond();
earlierSecond = passedTimeInSeconds%60+1;
currentSecond = Instant.now().getEpochSecond()% 60 + 1;
if(currentSecond > earlierSecond) {
System.out.println("Second: "+earlierSecond);
}
输出:
Second: 37
Second: 38
Second: 41
Second: 43
Second: 44
Second: 45
Second: 46
Second: 47
Second: 49
Second: 51
Second: 52
Second: 54
Second: 58
Second: 59
Second: 1
由于毫秒数,有时条件可能会失败。
我的情况是,从我的应用程序打开 waze 等待 25 秒,转到主屏幕,等待 10 秒,然后系统返回到 waze,然后我等待 15 秒然后再次进入主页并在那里等待 10 秒,
mainScreen.openWaze();
TimeWatch watch = TimeWatch.start();
double passedTimeInSeconds = 0;
System.out.println("start");
while (passedTimeInSeconds < 26.0) {
passedTimeInSeconds = watch.time(TimeUnit.SECONDS);
System.out.println("the seconds of 1 " + passedTimeInSeconds);
wazeInApp.validateWhereToField();
}
watch.reset();
passedTimeInSeconds = 0;
wazeInApp.goToHomeScreen();
while (passedTimeInSeconds < 11.0) {
passedTimeInSeconds = watch.time(TimeUnit.SECONDS);
System.out.println("the seconds of 2 " + passedTimeInSeconds);
wazeInApp.validateWhereToField();
}
watch.reset();
passedTimeInSeconds = 0;
while (passedTimeInSeconds < 16.0) {
passedTimeInSeconds = watch.time(TimeUnit.SECONDS);
System.out.println("the seconds 3 " + passedTimeInSeconds);
wazeInApp.validateWhereToField();
}
wazeInApp.goToHomeScreen();
我的控制台打印:
start
the seconds of 1 0.0
the seconds of 1 12.0
the seconds of 1 13.0
the seconds of 1 13.0
the seconds of 1 34.0
the seconds of 2 0.0
the seconds of 2 0.0
the seconds of 2 0.0
the seconds of 2 0.0
the seconds of 2 9.0
the seconds of 2 15.0
the seconds 3 0.0
the seconds 3 2.0
the seconds 3 4.0
the seconds 3 6.0
the seconds 3 9.0
the seconds 3 11.0
the seconds 3 13.0
the seconds 3 34.0
看起来很奇怪,打印的时间不对,而且超出了时间限制,而且这些是只是等待(无所事事)的正确解决方案吗? 谢谢
我相信您在这 26 秒内尝试检查某些东西,所以您不喜欢 Thread.sleep()
我尝试了以下方法并且对我有用。在这里,我将系统当前秒数加上 26(我们的预期时间),因此循环将从当前秒数开始迭代到 26 秒。
long passedTimeInSeconds = 0, earlierSecond = 0, currentSecond = 0;
long waitTillTime = Instant.now().getEpochSecond() +26;
while(passedTimeInSeconds < waitTillTime){
passedTimeInSeconds = Instant.now().getEpochSecond();
earlierSecond = passedTimeInSeconds%60+1;
currentSecond = Instant.now().getEpochSecond()% 60 + 1;
if(currentSecond > earlierSecond) {
System.out.println("Second: "+earlierSecond);
}
输出:
Second: 37
Second: 38
Second: 41
Second: 43
Second: 44
Second: 45
Second: 46
Second: 47
Second: 49
Second: 51
Second: 52
Second: 54
Second: 58
Second: 59
Second: 1
由于毫秒数,有时条件可能会失败。