设置计时器/不等待

hwazgwia  于 2021-09-13  发布在  Java
关注(0)|答案(1)|浏览(440)

我的情况是,从我的应用程序中,我打开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

看起来奇怪的是,打印的时间不正确,也超出了时间限制,而且这些是正确的解决方案吗?非常感谢。

0yycz8jy

0yycz8jy1#

我相信你正试图在这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

由于毫秒的原因,有时情况可能会失败。

相关问题