Execution limiter class may help in situations when something must be executed only once per specified time. Use cases:
Computing large data and want to print progress only every 10 seconds
Inputs are changing often but you do not wan’t to calculate result more often than once per second
Typical is to use variable storing time stamp of last execution and condition not allowing execution sooner than X. But that is not truly encapsulated solution, right? I use helper class, usage is following:
Class itself looks like:
There is also method reset(). It may be used for use case like:
Process files in directories and print current file every 5 seconds
Print at least one file per directory
In that case it may happen that second request won’t be fulfilled when processing time of some directories will be less than 5 seconds. Simple call limiter.reset(true) when entering directory will reset limiter and first tryExec will pass.
Using this tool, you can get rid of conditions in your code and make it cleaner and easier to understand.