Domanda

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!

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top