Search code examples
javatimefixed

How to call a method at exactly the same interval of time in a loop?


In Java, I have a loop which calls a method, and then that method has a sin which takes as parameter the time since the execution of the program. This loop runs at constant 60 updates per second (with the expected variations of +-1 or 2 ups). What I want to do is call a method within that loop, but that method must be called every 0.2, 0.4... seconds (fixed interval) , so what I did was: if(executionTime % 200 == 0) But that wasn't enough because it isn't always exact so it skipped a lot of times. For that matter I tried if(executionTime % 200 <= 50) to try and limit it, but it still skipped a few times, but less than before. So my question is if there is any way to have it run exactly at 0.2 seconds, with the following details:

  • If the loop takes more than 0.2 seconds for one iteration, then call the method.
  • If it takes less than 0.2 seconds, I'm not sure because if it is 30 ms, then obviously not, but if it is 198 ms then yes, within a small margin.

What I'm using this is to call sin with a variable that is incremented by some number every time the method is called (this is where the method must be called every 200 ms). Essentialy I want to do something similar to graphing a function that depends on time(X), where X is incremented by the same number every 200 ms of loop iteration. So, each loop iteration, a new point of the graph is calculated every 200 ms. What I could do is check before each call to other methods within the loop if it is time to call this method, but that ruins the order of the loop. I've searched around, but I can only find scheduling, which I believe doesn't do it.

EDIT: Pseudocode:

int increment;

while(running){
    first();
    second();

    if(condition)
        doSomething();
    if(timeCheck) // time check every 200ms; this is what I don't know how to do
        graphfunction(increment);
}

Solution

  • I would use the java.util.concurrent's ExecutorService. Take a look at the Javadoc. Use the following method on that object

    ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) 
    

    Javadoc:

    Creates and executes a periodic action that becomes enabled first after the given initial delay, and subsequently with the given period; that is executions will commence after initialDelay then initialDelay+period, then initialDelay + 2 * period, and so on.