javamultithreadingtimeunit

scheduleAtFixedRate method - change TimeUnit argument?


I'm using the SchduledExecuterService class to run some code repeatedly using the scheduleAtFixedRate() method.

exec.scheduleAtFixedRate(new Runnable() {
    public void run() {
        //some code
    }
}, 1, 1, TimeUnit.SECONDS);

The problem is that I want to alter the TimeUnit argument to be a certain fraction of a second, depending on a variable, i.e. TimeUnit.SECONDS*num; But the method doesn't take longs as parameters, only fixed TimeUnits. Any suggestions?


Solution

  • Why don't you play with period parameter ?

    long periodInMicroseconds = 16667;
    exec.scheduleAtFixedRate(new Runnable() {
        public void run() {
            //some code
        }
    }, 0, periodInMicroseconds, TimeUnit.MICROSECONDS);