문제

i'm having a routine that runs in a while(true) loop. After checking some variables with an if-statement , the class will call a method, but this method shall only be able to run once in 30 seconds. Let's say the if-statement returns true and the method gets called the method itself should check whether it was called in the last 30 seconds and only run if that's not the case.

I tried to google for stuff like "method timeout" but i couldn't find what i was searching for. Hope you guys know what i'm looking for.

Thank you in advance!

도움이 되었습니까?

해결책

Use a static variable with the timestamp of the last execution.

You can get this value with System.nanoTime().

Also, you should synchronize the block that makes the verification on a class lock, in case you're on a multithreaded environment.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top